From: Iain Barnett on 20 Jul 2010 08:34 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
From: Luis on 20 Jul 2010 09:04 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 -- 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
From: Iain Barnett on 20 Jul 2010 10:01 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
|
Pages: 1 Prev: How do I format return this value? Next: Count occurences of vars in array |