如何将yaml文件解析为ruby散列和/或数组?

时间:2023-01-15 09:22:33

I need to load a yaml file into Hash,
What should I do?

我需要将一个yaml文件加载到Hash中,我该怎么办?

4 个解决方案

#1


12  

Use the YAML module:
http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html

使用YAML模块:http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html

node = YAML::parse( <<EOY )
one: 1
two: 2
EOY

puts node.type_id
# prints: 'map'

p node.value['one']
# prints key and value nodes: 
#   [ #<YAML::YamlNode:0x8220278 @type_id="str", @value="one", @kind="scalar">, 
#     #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> ]'

# Mappings can also be accessed for just the value by accessing as a Hash directly
p node['one']
# prints: #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> 

http://yaml4r.sourceforge.net/doc/page/parsing_yaml_documents.htm

http://yaml4r.sourceforge.net/doc/page/parsing_yaml_documents.htm

#2


88  

I would use something like:

我会使用类似的东西:

hash = YAML.load(File.read("file_path"))

#3


7  

A simpler version of venables' answer:

更简单的venables答案:

hash = YAML.load_file("file_path")

#4


2  

You may run into a problem mentioned at this related question, namely, that the YAML file or stream specifies an object into which the YAML loader will attempt to convert the data into. The problem is that you will need a related Gem that knows about the object in question.

您可能遇到此相关问题中提到的问题,即YAML文件或流指定YAML加载程序将尝试将数据转换为的对象。问题是你需要一个知道相关对象的相关Gem。

My solution was quite trivial and is provided as an answer to that question. Do this:

我的解决方案非常简单,可作为该问题的答案。做这个:

yamltext = File.read("somefile","r")
yamltext.sub!(/^--- \!.*$/,'---')
hash = YAML.load(yamltext)

In essence, you strip the object-classifier text from the yaml-text. Then you parse/load it.

实质上,您从yaml-text中剥离对象分类器文本。然后你解析/加载它。

#1


12  

Use the YAML module:
http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html

使用YAML模块:http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html

node = YAML::parse( <<EOY )
one: 1
two: 2
EOY

puts node.type_id
# prints: 'map'

p node.value['one']
# prints key and value nodes: 
#   [ #<YAML::YamlNode:0x8220278 @type_id="str", @value="one", @kind="scalar">, 
#     #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> ]'

# Mappings can also be accessed for just the value by accessing as a Hash directly
p node['one']
# prints: #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> 

http://yaml4r.sourceforge.net/doc/page/parsing_yaml_documents.htm

http://yaml4r.sourceforge.net/doc/page/parsing_yaml_documents.htm

#2


88  

I would use something like:

我会使用类似的东西:

hash = YAML.load(File.read("file_path"))

#3


7  

A simpler version of venables' answer:

更简单的venables答案:

hash = YAML.load_file("file_path")

#4


2  

You may run into a problem mentioned at this related question, namely, that the YAML file or stream specifies an object into which the YAML loader will attempt to convert the data into. The problem is that you will need a related Gem that knows about the object in question.

您可能遇到此相关问题中提到的问题,即YAML文件或流指定YAML加载程序将尝试将数据转换为的对象。问题是你需要一个知道相关对象的相关Gem。

My solution was quite trivial and is provided as an answer to that question. Do this:

我的解决方案非常简单,可作为该问题的答案。做这个:

yamltext = File.read("somefile","r")
yamltext.sub!(/^--- \!.*$/,'---')
hash = YAML.load(yamltext)

In essence, you strip the object-classifier text from the yaml-text. Then you parse/load it.

实质上,您从yaml-text中剥离对象分类器文本。然后你解析/加载它。