开发者

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

开发者 https://www.devze.com 2023-04-08 04:51 出处:网络
If i have a method called roll (as in a dice) and it has a variable开发者_开发百科 called number.

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 ??


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
0

精彩评论

暂无评论...
验证码 换一张
取 消