【问题标题】:YAML Ruby Load multiple environment variablesYAML Ruby 加载多个环境变量
【发布时间】:2014-11-20 17:51:40
【问题描述】:

我继承了一个可以正常工作的工具,但是当我尝试扩展它时它却失败了。由于我是 ruby​​ 和 yaml 的新手,我真的不知道失败的原因是什么......

所以我有一个看起来像这样的类配置

  class Configuration
    def self.[] key
      @@config[key]
    end

    def self.load name
      @@config = nil
      io = File.open( File.dirname(__FILE__) + "/../../../config/config.yml" )
      YAML::load_documents(io) { |doc| @@config = doc[name] }
      raise "Could not locate a configuration named \"#{name}\"" unless @@config
    end

    def self.[]=key, value
      @@config[key] = value
    end

  end
end

raise "Please set the A environment variable" unless ENV['A']
Helpers::Configuration.load(ENV['A'])

raise "Please set the D environment variable" unless ENV['D']
Helpers::Configuration.load(ENV['D'])

raise "Please set the P environment variable" unless ENV['P']
Helpers::Configuration.load(ENV['P'])

所以我有一个环境变量 A 工作正常的第一个版本,然后当我想再集成 2 个环境变量时它失败了(它们是不同的键/值集)。我确实对其进行了调试,看起来当它读取第二个键/值时它会删除其他键/值(例如读取第三个键/值会删除前两个键/值,所以我最终得到@@config,只有第三个键/值 par 而不是我需要的所有值)。

这可能很容易解决,知道如何解决吗?

谢谢!

编辑: 配置文件使用如下所示:

Test:
  position_x: “56”
  position_y: “56”

现在我想让它像

“x56”:
  position_x: “56”

“x15”:
  position_x: “15”

“y56”:
  position_y: “56”

“y15”:
  position_y: “15”

我的想法是分别设置它们,我不需要创建所有组合......

【问题讨论】:

  • 您介意分享config.yml 文件吗?乍一看,您应该@@config = YAML::load 而不是您在那里做的事情。
  • 当然我的意思是这样做忘了包括它。它曾经很简单: 测试: position_x: “56” position_y: “56” 现在我想让它像 “x56”: position_x: “56” “x15”: position_x: “15” “y56”: position_y: “56” “y15”: position_y: “15” 我的想法是分别设置它们,我不需要创建所有组合......
  • 请更新您的帖子。

标签: ruby environment-variables yaml config environment


【解决方案1】:

每次调用load 时,都会删除之前的配置(在@@config = nil 行中)。如果您希望配置是所有文件的合并,您需要将新配置合并到现有配置中,而不是覆盖它。

类似这样的:

def self.load name
  @@config ||= {}
  io = File.open( File.dirname(__FILE__) + "/../../../config/config.yml" )
  YAML::load_documents(io) do |doc| 
    raise "Could not locate a configuration named \"#{name}\"" unless doc[name]
    @@config.merge!(doc[name])
  end
end

请注意,如果代码是按原样编写的,因为方法调用了不止一次,并且配置预计在读取之间重置,您将现在需要显式重置配置:

class Configuration

  # ...

  def reset_configuration
    @config = {}
  end
end

Helpers::Configuration.reset_configuration

raise "Please set the A environment variable" unless ENV['A']
Helpers::Configuration.load(ENV['A'])

raise "Please set the D environment variable" unless ENV['D']
Helpers::Configuration.load(ENV['D'])

raise "Please set the P environment variable" unless ENV['P']
Helpers::Configuration.load(ENV['P'])

【讨论】:

  • 完美,谢谢!我又想到了那个 nill,但即使删除它我也无法弄清楚如何将它们合并在一起!非常感谢。
【解决方案2】:

我会使用以下方式访问 YAML:

YAML::load_file(File.expand_path("../../../config/config.yml", File.dirname(__FILE__)))

expand_path 清理 '..' 链并返回清理后的版本,相对于 FILE。例如:

foo = '/path/to/a/file'
File.expand_path("../config.yml", File.dirname(foo)) # => "/path/to/config.yml"

load_file 读取并解析整个文件并返回。

【讨论】:

    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多