Croplio Croplio - 1 year ago 53
Ruby Question

How to parse a yaml file into ruby hashs and/or arrays?

I need to load a yaml file into Hash,

What should I do?

Answer Source

Use the YAML module:

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

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">