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

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 methodcall’ for nil:NilClass
(NoMethodError)

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

Any help is much appreciated

Regards,
Iain

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

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