Hash Inside Yaml File

Hash inside YAML file?

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

How to add values to a YAML hash using Ruby

There isn't much to using YAML in Ruby. I think you only need to know two methods in here : YAML.load and YAML.dump.

Assuming the file is file.yml with the contents you provided :

# YAML is part of the standard library.
require 'yaml'

# YAML.load parses a YAML string to appropriate Ruby objects.
# So you can first load the contents of the file with File#read,
# then parse it.
yaml_string = File.read "file.yml"
data = YAML.load yaml_string

# Now you have all of it in data.
data["apache_vhosts"]
# => {"webuser.co.uk"=>{"ip"=>"*", ...

# Once you are done manipulating them, dump it back with YAML.dump
# to convert it back to YAML.
output = YAML.dump data
File.write("file.yml", output)

That's pretty much it I think.

UPDATE

Okay now it's actually about appending already parsed data. By parsed I mean the data format parsed should be consistent with the existing format.

Assume that you have a valid parsed info on a new user named new_user :

new_user_info = {"ensure"=>"present", "gid"=>"900", "managehome"=>true, "home"=>"/home/new_user"}

To append it to the original YAML contents (parsed into ruby objects), you can do this :

data["users"]["new_user"] = new_user_info

Once dumped, this will add another user entry named new_user at the bottom of the users list (under users: on YAML file). Hosts can be added in the same way too, once you get the domain name and other info, you can add them like this :

data["apache_vhosts"]["new_domain_name"] = info

Again it's important to have the information arranged in the right hierarchy.

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

Use the YAML module:

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

Rails load YAML to hash and reference by symbol

Try using the HashWithIndifferentAccess like

APP_CONFIG = HashWithIndifferentAccess.new(YAML.load(File.read(File.expand_path('../app.yml', __FILE__))))

Convert Ruby Hash into YAML

Here's how I'd do it:

require 'yaml'

HASH_OF_HASHES = {
"hostname1.test.com"=> {"public"=>"51", "private"=>"10"},
"hostname2.test.com"=> {"public"=>"192", "private"=>"12"}
}

ARRAY_OF_HASHES = [
{"hostname1.test.com"=> {"public"=>"51", "private"=>"10"}},
{"hostname2.test.com"=> {"public"=>"192", "private"=>"12"}}
]

puts HASH_OF_HASHES.to_yaml
puts
puts ARRAY_OF_HASHES.to_yaml

Which outputs:

---
hostname1.test.com:
public: '51'
private: '10'
hostname2.test.com:
public: '192'
private: '12'

---
- hostname1.test.com:
public: '51'
private: '10'
- hostname2.test.com:
public: '192'
private: '12'

The Object class has a to_yaml method. I used that and it generated the YAML file correctly.

No, it doesn't.

This is from a freshly opened IRB session:

Object.instance_methods.grep(/to_yaml/)
=> []
require 'yaml'
=> true
Object.instance_methods.grep(/to_yaml/)
=> [:psych_to_yaml, :to_yaml, :to_yaml_properties]


Related Topics



Leave a reply



Submit