ruby 如何在我的 .rb 文件中共享变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8334684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:32:41  来源:igfitidea点击:

How to share variables across my .rb files?

rubyinclude

提问by Radek

I have a few .rbfiles and I want to use the same variables in all of them. Let's say variable test_variable = "test"should be accessible from all my .rbfiles. How can I achieve that?

我有几个.rb文件,我想在所有文件中使用相同的变量。假设变量test_variable = "test"应该可以从我的所有.rb文件中访问。我怎样才能做到这一点?

I created settings.rbfile with test_variable = "test"then used require 'settings'in another .rbfile, but it didn't work. I would like to use requirenot load.

我创建了settings.rb文件,test_variable = "test"然后require 'settings'在另一个.rb文件中使用,但它不起作用。我想用requirenot load

I tried to make the variable global by prefixing the variable name with $, but I am still getting undefined local variable or method 'test_variable' for main:Object (NameError).

我试图通过在变量名前加上前缀来使变量成为全局变量$,但我仍然得到undefined local variable or method 'test_variable' for main:Object (NameError).

回答by Phrogz

  1. Constants (which include modules and classes) are added to the shared global environment:

    phrogz$ cat constants1.rb 
    TEST_VARIABLE = "test"
    
    phrogz$ cat constants2.rb 
    require_relative 'constants1'
    p TEST_VARIABLE
    
    phrogz$ ruby constants2.rb 
    "test"
    
  2. Instance variables declared in main are all part of the same main:

    phrogz$ cat instance1.rb 
    @test_variable = "test"
    
    phrogz$ cat instance2.rb 
    require_relative 'instance1'
    p @test_variable
    
    phrogz$ ruby instance2.rb 
    "test"
    
  3. Global variables are also all part of the same environment (tested in 1.8.6, 1.8.7, and 1.9.2):

    phrogz$ cat global1.rb 
    $test_variable = "test"
    
    phrogz$ cat global2.rb 
    require_relative 'global1'
    p $test_variable, RUBY_DESCRIPTION
    
    phrogz$ ruby global2.rb 
    "test"
    "ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]"
    
  1. 常量(包括模块和类)被添加到共享的全局环境中:

    phrogz$ cat constants1.rb 
    TEST_VARIABLE = "test"
    
    phrogz$ cat constants2.rb 
    require_relative 'constants1'
    p TEST_VARIABLE
    
    phrogz$ ruby constants2.rb 
    "test"
    
  2. 在 main 中声明的实例变量都是相同的一部分main

    phrogz$ cat instance1.rb 
    @test_variable = "test"
    
    phrogz$ cat instance2.rb 
    require_relative 'instance1'
    p @test_variable
    
    phrogz$ ruby instance2.rb 
    "test"
    
  3. 全局变量也是同一个环境的一部分(在 1.8.6、1.8.7 和 1.9.2 中测试过):

    phrogz$ cat global1.rb 
    $test_variable = "test"
    
    phrogz$ cat global2.rb 
    require_relative 'global1'
    p $test_variable, RUBY_DESCRIPTION
    
    phrogz$ ruby global2.rb 
    "test"
    "ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]"
    

回答by Linuxios

Ruby will nevershare local variables between files. You can wrap them in a Module though:

Ruby永远不会在文件之间共享局部变量。您可以将它们包装在一个模块中:

module SharedVaribles
  @test_var="Hello, World"

  def self.test_var
    return @test_var
  end

  def self.test_var=(val)
    @test_val=val;
  end
end

Put that in settings.rb, requireit into all your files, and use SharedVaribles.test_varand SharedVaribles.test_var=to access the variable. Remember, Ruby's requireis nothing like C's #include, it is much more complex. It executes the file, then imports all constants, modules, and classes to the requireer.

把在settings.rbrequire它为您的所有文件,并使用SharedVaribles.test_varSharedVaribles.test_var=访问的变量。请记住,Rubyrequire与 C 完全不同#include,它要复杂得多。它执行文件,然后将所有常量、模块和类导入到requireer。

回答by logical_proof

module Foo
  attr_accessor :test_var
  def initialize
    @test_var = "hello world"
  end
end

Create the module with your variable or variables in your config file and then include the module in whatever class you intend to use.

在配置文件中使用您的一个或多个变量创建模块,然后将该模块包含在您打算使用的任何类中。

require_relative 'foomod.rb'
class Bar
  include Foo
end

foobar = Bar.new
foobar.test_var = "goodbye"
puts foobar.test_var

Each instance of the class will initialize with whatever value you would like.

该类的每个实例都将使用您想要的任何值进行初始化。

回答by Gerard Simpson

You shouldn't need to wrap these variables in a module.

您不需要将这些变量包装在module.

Simply adding them to another_file.rband using require_relative 'another_file'(if it's in the same directory) or require 'path/to/another_file'should be sufficient to share those variables across files.

简单地将它们添加到another_file.rb并使用require_relative 'another_file'(如果它在同一目录中)或者require 'path/to/another_file'应该足以跨文件共享这些变量。