# Undefined method \`call' for nil:NilClass (NoMethodError)

**URL:** <https://www.ruby-forum.com/t/undefined-method-call-for-nil-nilclass-nomethoderror/191901>\
**Category:** Ruby\
**Created:** [July 20, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/undefined-method-call-for-nil-nilclass-nomethoderror/191901 "2010-07-20T14:39:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 20, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/undefined-method-call-for-nil-nilclass-nomethoderror/191901/1 "2010-07-20T14:39:59Z")

</div>

Hello,

Just playing around with a few things in Ruby, and I’m wondering why the  
following script would throw an error? I’m using Ruby 1.9.1 on OSX 10.6

#!/usr/bin/env ruby -wKU

puts “Welcome”

h = { ‘a’ =\> lambda { puts “again again!” } ,  
‘b’ =\> lambda { puts “oh no!” }  
}

while true  
x = STDIN.getc  
break if x == ‘q’  
h[x].call  
end

$ ./getchar.rb  
Welcome  
a  
again again!  
./getchar.rb:12:in `<main>': undefined method`call’ for nil:NilClass  
(NoMethodError)

$ ./getchar.rb  
Welcome  
b  
on no!  
./getchar.rb:12:in `<main>': undefined method`call’ for nil:NilClass  
(NoMethodError)

Any help is much appreciated

Regards,  
Iain

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 20, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/undefined-method-call-for-nil-nilclass-nomethoderror/191901/2 "2010-07-20T15:04:35Z")

</div>

El 20/07/10 08:34, Iain B.  
escribió:\> }

> again again!  
> Any help is much appreciated
> 
> Regards,  
> Iain

the error is when you hit ‘b’+enter, there are two character, character  
‘b’ and the character ‘enter’ which do not is present in hash h

---

<div class="post-metadata">

**Author:** ![tensaiji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/6de8d8/32.png) [@tensaiji](https://www.ruby-forum.com/u/tensaiji)\
**Post date:** [July 20, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/undefined-method-call-for-nil-nilclass-nomethoderror/191901/3 "2010-07-20T16:10:47Z")

</div>

On 20 Jul 2010, at 14:04, Luis wrote:

> the error is when you hit ‘b’+enter, there are two character, character ‘b’ and the character ‘enter’ which do not is present in hash h
> 
> –

ok, thanks. x.chomp! fixed it.

Iain
