将值插入到 YAML 转储的哈希中

发布于 2024-10-05 15:19:39 字数 448 浏览 4 评论 0原文

我正在创建一个哈希,该哈希最终将在 YAML 中转储到磁盘上,但我需要捕获存储在磁盘上文件中的多个值并将它们插入到哈希中。我可以成功创建一个带有逗号分隔值的变量,但我需要将这些值插入到我的“classes”键中:

variable_values = "class1,class2,class3"

最终,我需要将它们放入我的测试哈希中,以便它模拟如下内容:

test_hash = {'Classes' => ['class1', 'class2', 'class3']}

最后,我可以输出它们到 yaml ,它看起来像这样:

--- 
classes: 
- class1
- class2
- class3

迭代值并将它们插入哈希的最佳方法是什么?感谢您提供的任何帮助!

I'm creating a hash that will eventually be dumped on disk in YAML, but I need to capture multiple values stored in a file on disk and insert them into a hash. I can successfully create a variable with comma separated values, but I need to insert those values into a my "classes" key:

variable_values = "class1,class2,class3"

Ultimately, I need to get them into my test hash so it simulates something like this:

test_hash = {'Classes' => ['class1', 'class2', 'class3']}

Finally, I can output them to yaml so it looks like this:

--- 
classes: 
- class1
- class2
- class3

What's the best way to iterate through the values and insert them into the hash? Thanks for any help you can offer!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-10-12 15:19:39

你可能想要这样的东西:

test_hash = {'Classes' => variable_values.split(',')}

You'd probably want something like:

test_hash = {'Classes' => variable_values.split(',')}
中二柚 2024-10-12 15:19:39

如果您想要序列化 ​​Ruby 类(我无法确定),您可能需要以下代码(由 opensoul.org,以及 Small Eigen Collider)

class Module
  yaml_as "tag:ruby.yaml.org,2002:module"

  def Module.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
    else
      raise YAML::TypeError, "Invalid Module: " + val.inspect
    end
  end

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:module", self.name, :plain )
    }
  end
end

class Class
  yaml_as "tag:ruby.yaml.org,2002:class"

  def Class.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
    else
      raise YAML::TypeError, "Invalid Class: " + val.inspect
    end
  end

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain )
    }
  end
end

如果您尝试序列化/反序列化匿名类,代码当前会引发异常(我可以修复此问题,但不需要),除此之外它对我来说效果很好。

If you're wanting to serialize Ruby Classes (I'm not able to tell for sure), you'll probably want the following code (courtesy of opensoul.org, and as used in the Small Eigen Collider)

class Module
  yaml_as "tag:ruby.yaml.org,2002:module"

  def Module.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
    else
      raise YAML::TypeError, "Invalid Module: " + val.inspect
    end
  end

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:module", self.name, :plain )
    }
  end
end

class Class
  yaml_as "tag:ruby.yaml.org,2002:class"

  def Class.yaml_new( klass, tag, val )
    if String === val
      val.split(/::/).inject(Object) {|m, n| m.const_get(n)}
    else
      raise YAML::TypeError, "Invalid Class: " + val.inspect
    end
  end

  def to_yaml( opts = {} )
    YAML::quick_emit( nil, opts ) { |out|
      out.scalar( "tag:ruby.yaml.org,2002:class", self.name, :plain )
    }
  end
end

The code currently throws an exception if you try to serialize/deserialize anonymous classes (something I could fix but don't need to), and apart from that it works well for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文