Skip to content

Instantly share code, notes, and snippets.

@shivabhusal
Last active June 27, 2017 11:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shivabhusal/f407dfd0d05912c0eae272d7a8569a76 to your computer and use it in GitHub Desktop.
Run this code in your machine and find the bug
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
Copy link

class Roulette
  def method_missing(name, *args)
    person = name.to_s.capitalize
    number = 0
    3.times do
      number = rand(10) + 1
      puts "#{number}..."
    end
    "#{person} got a #{number}"
  end
end

number_of = Roulette.new
puts number_of.bob
puts number_of.frank

@aditya-kapoor
Copy link

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

@narutoo9x
Copy link

narutoo9x commented Jun 27, 2017

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