将值插入到 YAML 转储的哈希中
我正在创建一个哈希,该哈希最终将在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可能想要这样的东西:
You'd probably want something like:
如果您想要序列化 Ruby 类(我无法确定),您可能需要以下代码(由 opensoul.org,以及 Small Eigen Collider)
如果您尝试序列化/反序列化匿名类,代码当前会引发异常(我可以修复此问题,但不需要),除此之外它对我来说效果很好。
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)
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.