【发布时间】:2015-04-22 08:58:45
【问题描述】:
我的主程序中有一个局部变量。现在我想给它一个方法,这样它就可以用它来工作了。 最好的方法是什么?一个实例变量@test?
【问题讨论】:
-
通过示例从here 了解
Scope
我的主程序中有一个局部变量。现在我想给它一个方法,这样它就可以用它来工作了。 最好的方法是什么?一个实例变量@test?
【问题讨论】:
Scope
您可以将该变量作为方法参数,如下例所示:
def main_program
my_variable = 3
other_method(my_variable)
end
def other_method(special_variable)
# do something with the special-variable
end
可以在Ruby programming wiki 中找到有关如何定义和调用方法的精彩阅读
【讨论】:
other_method(var1, var2)。我建议你在开始使用 Ruby 编程之前先从一些基础开始。