Hi, I have a hash in a controller file which I have to write into a YAML.yml file in a particular format. This is the code I am using in the controller to write to yml file. "File.open("config/locales/output.yml", "w") {|f| f.write(hash_data.to_yaml) }" output.yml is the yml file. hash_data is my hash which has a key having a hash to array of values. Current output; --- ''01'': - red - green Expected output: en: 01: "red, green" Is this possible? Regards, Nikhil -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Nicolas Desprès
2012-Dec-19 10:04 UTC
Re: Writing data into yml file in a particular format
On Wed, Dec 19, 2012 at 6:37 AM, nikhil rn <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, >Hi,> > I have a hash in a controller file which I have to write into a YAML.yml > file in a particular format. > > This is the code I am using in the controller to write to yml file. > > "File.open("config/locales/output.yml", "w") {|f| > f.write(hash_data.to_yaml) }" > > output.yml is the yml file. > hash_data is my hash which has a key having a hash to array of values. > > Current output; > --- > ''01'': > - red > - green > > > Expected output: > en: > 01: "red, green" >> Is this possible? >You could convert your array to a string before to call .to_yaml: [ ''red'', ''green'' ].join('', '') Cheers, Nico -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Wednesday, 19 December 2012 00:37:23 UTC-5, Ruby-Forum.com User wrote:> > Hi, > > I have a hash in a controller file which I have to write into a YAML.yml > file in a particular format. > > This is the code I am using in the controller to write to yml file. > > "File.open("config/locales/output.yml", "w") {|f| > f.write(hash_data.to_yaml) }" > > output.yml is the yml file. > hash_data is my hash which has a key having a hash to array of values. > > Current output; > --- > ''01'': > - red > - green > > > Expected output: > en: > 01: "red, green"It''s certainly possible, but if you want the output YAML to look like that you''re going to need to put the *input* data into the same sort of format. For instance, running the expected output (with quotes around 01, to avoid it being parsed as an integer) through YAML.load gives this Ruby result: {"en"=>{"01"=>"red, green"}} --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dzXcc883ltEJ. For more options, visit https://groups.google.com/groups/opt_out.