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

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


I would use something like:

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


A simpler version of venables' answer:

hash = YAML.load_file("file_path")


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

node = YAML::parse( <<EOY )one: 1two: 2EOYputs 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 directlyp node['one']# prints: #<YAML::YamlNode:0x821fcd8 @type_id="int", @value="1", @kind="scalar"> 

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