The file welcome.rb
contains:
文件的欢迎。rb包含:
welcome_message = "hi there"
But in IRB, I can't access the variable I just created:
但在IRB中,我无法访问刚才创建的变量:
require './welcome.rb'
puts welcome_message
# => undefined local variable or method `welcome_message' for main:Object
What is the best way to bring in predefined variables and have initialization work done when you require
something into your IRB session? Global variables don't seem like the right path.
在IRB会话中需要一些东西时,引入预定义变量并完成初始化工作的最佳方式是什么?全局变量看起来不是正确的路径。
4 个解决方案
#1
14
While it is true that you cannot access local variables defined in required files, you can access constants, and you can access anything stored in an object that you have access to in both contexts. So, there are a few ways to share information, depending on your goals.
虽然您确实不能访问在必需文件中定义的本地变量,但是您可以访问常量,并且可以访问存储在对象中的任何内容,您可以访问在两个上下文中都可以访问的内容。因此,根据你的目标,有几种分享信息的方式。
The most common solution is probably to define a module and put your shared value in there. Since modules are constants, you'll be able to access it in the requiring context.
最常见的解决方案可能是定义一个模块并将共享值放在其中。由于模块是常量,您将能够在需要的上下文中访问它。
# in welcome.rb
module Messages
WELCOME = "hi there"
end
# in irb
puts Messages::WELCOME # prints out "hi there"
You could also put the value inside a class, to much the same effect. Alternatively, you could just define it as a constant in the file. Since the default context is an object of class Object, referred to as main, you could also define a method, instance variable, or class variable on main. All of these approaches end up being essentially different ways of making "global variables," more or less, and may not be optimal for most purposes. On the other hand, for small projects with very well defined scopes, they may be fine.
您还可以将值放在类中,效果大致相同。或者,您可以将它定义为文件中的常量。由于默认上下文是类对象的对象,称为main,您还可以在main上定义方法、实例变量或类变量。所有这些方法最终都是以不同的方式生成“全局变量”,或多或少,并且可能不是最理想的。另一方面,对于定义很好的小项目来说,它们可能是好的。
# in welcome.rb
WELCOME = "hi constant"
@welcome = "hi instance var"
@@welcome = "hi class var"
def welcome
"hi method"
end
# in irb
# These all print out what you would expect.
puts WELCOME
puts @welcome
puts @@welcome
puts welcome
#2
3
You can't access local variables defined in the included file. You can use ivars:
您不能访问所包含文件中定义的本地变量。您可以使用实例变量:
# in welcome.rb
@welcome_message = 'hi there!'
# and then, in irb:
require 'welcome'
puts @welcome_message
#=>hi there!
#3
2
I think the best way is to define a class like this
我认为最好的方法是定义这样的类
class Welcome
MESSAGE = "hi there"
end
then in irb you can call your code like this :
在irb中,你可以这样调用你的代码:
puts Welcome::MESSAGE
#4
2
That should at least enable the experience from irb:
这至少应该能使irb的经验:
def welcome_message; "hi there" end
#1
14
While it is true that you cannot access local variables defined in required files, you can access constants, and you can access anything stored in an object that you have access to in both contexts. So, there are a few ways to share information, depending on your goals.
虽然您确实不能访问在必需文件中定义的本地变量,但是您可以访问常量,并且可以访问存储在对象中的任何内容,您可以访问在两个上下文中都可以访问的内容。因此,根据你的目标,有几种分享信息的方式。
The most common solution is probably to define a module and put your shared value in there. Since modules are constants, you'll be able to access it in the requiring context.
最常见的解决方案可能是定义一个模块并将共享值放在其中。由于模块是常量,您将能够在需要的上下文中访问它。
# in welcome.rb
module Messages
WELCOME = "hi there"
end
# in irb
puts Messages::WELCOME # prints out "hi there"
You could also put the value inside a class, to much the same effect. Alternatively, you could just define it as a constant in the file. Since the default context is an object of class Object, referred to as main, you could also define a method, instance variable, or class variable on main. All of these approaches end up being essentially different ways of making "global variables," more or less, and may not be optimal for most purposes. On the other hand, for small projects with very well defined scopes, they may be fine.
您还可以将值放在类中,效果大致相同。或者,您可以将它定义为文件中的常量。由于默认上下文是类对象的对象,称为main,您还可以在main上定义方法、实例变量或类变量。所有这些方法最终都是以不同的方式生成“全局变量”,或多或少,并且可能不是最理想的。另一方面,对于定义很好的小项目来说,它们可能是好的。
# in welcome.rb
WELCOME = "hi constant"
@welcome = "hi instance var"
@@welcome = "hi class var"
def welcome
"hi method"
end
# in irb
# These all print out what you would expect.
puts WELCOME
puts @welcome
puts @@welcome
puts welcome
#2
3
You can't access local variables defined in the included file. You can use ivars:
您不能访问所包含文件中定义的本地变量。您可以使用实例变量:
# in welcome.rb
@welcome_message = 'hi there!'
# and then, in irb:
require 'welcome'
puts @welcome_message
#=>hi there!
#3
2
I think the best way is to define a class like this
我认为最好的方法是定义这样的类
class Welcome
MESSAGE = "hi there"
end
then in irb you can call your code like this :
在irb中,你可以这样调用你的代码:
puts Welcome::MESSAGE
#4
2
That should at least enable the experience from irb:
这至少应该能使irb的经验:
def welcome_message; "hi there" end