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

I

Iain Barnett

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 =3D { 'a' =3D> lambda { puts "again again!" } ,
'b' =3D> lambda { puts "oh no!" }
}

while true
x =3D STDIN.getc
break if x =3D=3D '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
 
L

Luis

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top