如何从类中定义irb控制台中存在的变量?
我想完成这样的事情:
class MyClass
def self.define_var_in_irb
# of course this doesn't work because a is a local variable,
# not in the irb scope, and thats my question, how I can define
# a variable into the current irb session from inside a class.
a = 'hello'
end
end
>> MyClass.define_var_in_irb
>> puts a
# 'hello'
答案 0 :(得分:1)
如果您的唯一目标是设置一些控制台变量,那么可以使用其他工具。
如果你使用pry
(你应该使用BTW),那么你就可以使用.pryrc
了。在那里创建你的局部变量。
r = Revision.find whatever
Pry启动时将拾取并评估此文件。只需将它放在您的项目目录(或HOME)中。
IRB使用.irbrc
文件,但不会从中公开局部变量。但是,实例变量将起作用。此外,它不会加载本地文件(仅来自HOME的全局文件)