Forum: Ruby on Rails Writing data into yml file in a particular format

Posted by nikhil rn (nikhilrkn)
on 2012-12-19 06:37
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 by "Nicolas Desprès" <nicolas.despres@gmail.com> (Guest)
on 2012-12-19 11:06
(Received via mailing list)
On Wed, Dec 19, 2012 at 6:37 AM, nikhil rn <lists@ruby-forum.com> wrote:

> Hi,
>

Hi,


> hash_data is my hash which has a key having a hash to array of values.
>   01: "red, green"
>

> Is this possible?
>

You could convert your array to a string before to call .to_yaml: [ 
'red',
'green' ].join(', ')

Cheers,
Nico
Posted by Matt Jones (Guest)
on 2012-12-19 23:08
(Received via mailing list)
On Wednesday, 19 December 2012 00:37:23 UTC-5, Ruby-Forum.com User 
wrote:
>
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.