Convert Array / Hash in YAML to Ruby on Rails

I want to build the following YAML formatting from an array / hash:

Name: gender: - female nationality: - german - danish 

Now I have such an array:

 names = ["Abbie", "Abeline", "Abelone"] 

What would be the easiest way to get from this array to YAML?

I tried converting it to a hash by adding values ​​for gender and nationality:

 names.each do |name| (META_HASH ||= Hash.new) = name => { gender: 'female', nationality: ['german', 'danish'] } end 

This, however, just gives me a syntax error. Any help in converting this would be much appreciated!

+7
arrays ruby ruby-on-rails yaml hash
source share
1 answer
 > require 'yaml' > names = ["Abbie", "Abeline", "Abelone"] => ["Abbie", "Abeline", "Abelone"] > puts names.to_yaml --- - Abbie - Abeline - Abelone => nil > h = {:name => { gender: 'female', nationality: ['german', 'danish'] }} => {:name=>{:gender=>"female", :nationality=>["german", "danish"]}} > puts h.to_yaml --- :name: :gender: female :nationality: - german - danish => nil > a = names.map { |n| { n => { gender: 'female', nationality: ['german', 'danish'] } } } > puts a.to_yaml --- - Abbie: :gender: female :nationality: - german - danish - Abeline: :gender: female :nationality: - german - danish - Abelone: :gender: female :nationality: - german - danish => nil 
+11
source share

All Articles