This short code is working when I'm using class variable @@points instead of @points. I wonder why it's happening like this? Someone can explain me? It looks like @points is always nil.
class Game
@points = 0
def start
until @points == 10
puts "Guess number between 0 and 10:"
num = gets.chomp.to_i
break if @points == 0
guess_number(num)
puts "Your score is: #{@points}"
end
end
def guess_number(num)
@points += 1 if num == rand(0..10)
end
end
game = Game.new
game.start
gets.chomp.to_iis often written withoutchomp:gets.to_i. That's because"123".to_i,"123\n".to_iand"123X456abc".to_iall return123. See String#to_i, which includes, "Extraneous characters past the end of a valid number are ignored. ".