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 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

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

···

El 20/07/10 08:34, Iain Barnett escribió:

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.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

--
Este mensaje ha sido analizado por MailScanner
en busca de virus y otros contenidos peligrosos,
y se considera que está limpio.
For all your IT requirements visit: http://www.transtec.co.uk

ok, thanks. x.chomp! fixed it.

Iain

···

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

--