YAML 文件内的哈希值?
我想在使用以下命令解析的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像这样标记它,
注意这里的间距很重要。 “-”必须缩进一个空格(不是制表符),后跟一个空格。和
url
&label
必须缩进两个空格(也不能缩进制表符)。此外,这可能会有所帮助: http://www.yaml.org/YAML_for_ruby.html
You can mark it up like this
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
Ceilingfish 的答案在技术上可能是正确的,但他建议在行尾使用空格。这很容易出错,而且不是一个好的做法!
我将这样做:
创建一个包含以下内容的 settings.yaml 文件:
这将在加载 YAML 文件后创建以下哈希:
在本示例中,我还使用符号,因为这似乎是首选的方式在 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:
This will create the following hash after the YAML file has been loaded:
In this example, I also use symbols since this seems to be the preferred way of storing Ruby keys in Ruby.
老问题,但由于我处于类似的位置......就像贾斯珀指出的那样,Ceilingfish 的答案是正确的。但您也可以避免
依赖破折号后的尾随空格。
Old question, but since I was in a similar spot... Like Jasper pointed out, Ceilingfish's answer is correct. But you can also do
to avoid having to rely on trailing whitespace after the dash.