Ruby Program Error: NoMethodError
I typed up a 开发者_JS百科simple Ruby code for a tutorial question, as shown below.
#Grandma is deaf!
puts "Hey Sonny! It's your lovely Grandmother! How are you?"
response = gets.chomp
while response != "BYE"
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
else
puts "NO! NOT SINCE " + (1930 + rand(21)).to_s + "!"
end
response = gets.chomp
end
puts "GOOD BYE, SONNY!"
However, when I run this, the window displays:
Hey Sonny! It's your lovely Grandmother! How are you?
NoMethodError: private method ‘chomp’ called for nil:NilClass
at top level in deafGrandma.rb at line 3
I don't understand why chomp
is not recognized. I'm using textMate on a Mac I have Ruby version 1.8.7, which should be fine. Any solutions?
Thank you so much :)
Adrian is right about interactive input being disabled in TextMate 1.5.9 (r1510). See this post from TextMate's developer.
However, you can upgrade to a "cutting-edge" TextMate release that restores interactive input, and will allow you to run the above code just fine. Go to TextMate's Preferences -> Software Updates
and make sure Automatically check for updates
is checked.
Select Cutting-Edge
in the Watch For:
dropdown menu. Finally, click Check Now
. The latest release (r1589) should automatically download. Interactive input is re-enabled in this release.
If you are using the Cmd-R shortcut in TextMate to run your code, you will not be able to supply it input because textmate only supports output. You will have to run it in a terminal instead. The reason you are getting that error is because $stdin
is closed, so gets
returns nil
.
精彩评论