I’m really beginner in ruby, trying to write a simple program to detect uppercase input. I use ruby-1.9.3-p125 now. So, I’m trying to compile this:
# coding: utf-8
puts 'hello! enter something:'
while req!=req.upcase
req=gets.chomp
if req == req.upcase
puts "This is UpperCase!"
else
puts "Not UpperCase :( Try again!"
end
end
puts "GoodBye!"
And I get a such error:
app1.rb:4:in `<main>': undefined method `upcase' for nil:NilClass (NoMethodError)
Maybe I should include any lib or smth like this? BTW, “UpCase”.upcase` works good.
Your while loop references req before anything is assigned to it.
You could set ref in your while condition
while (req = gets.chomp) != req.upcase, but this makes the condition complicated and still doesn’t handle the end of file condition where gets returns nil. Better would be to make the condition focus on end of file and use break to terminate the loop in your special test: