YAML 文件内的哈希值?

发布于 2024-09-05 07:35:45 字数 464 浏览 2 评论 0原文

我想在使用以下命令解析的 YAML 文件中包含哈希和列表:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

我的 YAML 文件如下所示:

feeds: [{:url => 'http://www.google.com', :label => 'default'}]

但这似乎不起作用。 我将如何实现这样的目标?

谢谢, 尤瓦尔


编辑:抱歉,伙计们。我仍然不清楚如何做到这一点,我怀疑这部分是由于我的措辞有些模糊。我在此处提出了一个措辞更好、更广泛的问题。谢谢你!

I want to include a hash and list inside a YAML file that I'm parsing with the following command:

APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")

My YAML file looks like this:

feeds: [{:url => 'http://www.google.com', :label => 'default'}]

But this doesn't seem to work.
How would I go about achieving such a thing?

Thanks,
Yuval


EDIT: Sorry, guys. I'm still unclear about how to do this and I suspect it is in part due to my somewhat vague phrasing. I asked a better-phrased, more broad question here. Thank you!

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

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

发布评论

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

评论(3

忆伤 2024-09-12 07:35:45

您可以像这样标记它,

feeds:
 - 
  url: 'http://www.google.com'
  label: 'default'

注意这里的间距很重要。 “-”必须缩进一个空格(不是制表符),后跟一个空格。和 url & label 必须缩进两个空格(也不能缩进制表符)。

此外,这可能会有所帮助: http://www.yaml.org/YAML_for_ruby.html

You can mark it up like this

feeds:
 - 
  url: 'http://www.google.com'
  label: 'default'

Note the spacing is important here. "-" must be indented by a single space (not a tab), and followed by a single space. And url & label must be indented by two spaces (not tabs either).

Additionally this might be helpful: http://www.yaml.org/YAML_for_ruby.html

╭⌒浅淡时光〆 2024-09-12 07:35:45

Ceilingfish 的答案在技术上可能是正确的,但他建议在行尾使用空格。这很容易出错,而且不是一个好的做法!

我将这样做:

创建一个包含以下内容的 settings.yaml 文件:

---
feeds:
  :url: 'http://www.google.com'
  :label: 'default'

这将在加载 YAML 文件后创建以下哈希:

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML.load_file('settings.yaml')
=> {"feeds"=>{:url=>"http://www.google.com", :label=>"default"}}
irb(main):003:0> 

在本示例中,我还使用符号,因为这似乎是首选的方式在 Ruby 中存储 Ruby 密钥。

Ceilingfish's answer is maybe technically correct, but he recommends to use a white space at the end of a line. This is prone to errors and is not a good practice!

This is how I would do it:

Create a settings.yaml file with the following contents:

---
feeds:
  :url: 'http://www.google.com'
  :label: 'default'

This will create the following hash after the YAML file has been loaded:

irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML.load_file('settings.yaml')
=> {"feeds"=>{:url=>"http://www.google.com", :label=>"default"}}
irb(main):003:0> 

In this example, I also use symbols since this seems to be the preferred way of storing Ruby keys in Ruby.

强辩 2024-09-12 07:35:45

老问题,但由于我处于类似的位置......就像贾斯珀指出的那样,Ceilingfish 的答案是正确的。但您也可以避免

feeds:
 - url: 'http://www.google.com'
   label: 'default'

依赖破折号后的尾随空格。

Old question, but since I was in a similar spot... Like Jasper pointed out, Ceilingfish's answer is correct. But you can also do

feeds:
 - url: 'http://www.google.com'
   label: 'default'

to avoid having to rely on trailing whitespace after the dash.

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