Last active
June 27, 2017 11:49
Star
You must be signed in to star a gist
Run this code in your machine and find the bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Roulette | |
def method_missing(name, *args) | |
person = name.to_s.capitalize | |
3.times do | |
number = rand(10) + 1 | |
puts "#{number}..." | |
end | |
"#{person} got a #{number}" | |
end | |
end | |
# You can use the Roulette like this: | |
number_of = Roulette.new | |
puts number_of.bob | |
puts number_of.frank | |
# Expected output | |
# 5... | |
# 6... | |
# 10... | |
# Frank got a 10 | |
# 7... | |
# 4... | |
# 3... | |
# Bob got a 3 |
kuldeepaggarwal
commented
Jun 25, 2017
class Roulette
def method_missing(name, *args)
3.times do
@number = rand(10) + 1
puts "#{@number}..."
end
"#{name.to_s.capitalize} got a #{@number}"
end
end
number_of = Roulette.new
puts number_of.bob
puts number_of.frank
the number
variable doesn't exist out of the loop block.
You should use instance variable @number
or define number
before the loop block.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment