gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:57:24 26 4
gpt4 key购买 nike

如果我有一个名为 roll 的方法(如在骰子中)并且它有一个名为 number 的变量。

同一个类中的另一个名为 stats 的方法可以使用其中的那个变量吗??

最佳答案

你是说这样?

class Die
def roll
@number = 5
end

def stats
puts @number
end
end

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

关于 ruby : if i declare a variable in a method does another method in the same class know it exists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com