Ger Crowley
Ger Crowley

Reputation: 891

ruby : if i declare a variable in a method does another method in the same class know it exists?

If i have a method called roll (as in a dice) and it has a variable called number.

can another method in the same class called stats use that variable in it ??

Upvotes: 3

Views: 92

Answers (1)

Brian Genisio
Brian Genisio

Reputation: 48147

You mean like this?

class Die
  def roll
    @number = 5
  end

  def stats
    puts @number
  end
end

d = Die.new
d.roll
d.stats # prints 5

Upvotes: 4

Related Questions