How to output inline list by to_yaml

M

meruby

require 'yaml'
l = [1, 2, 3]
l.to_yaml
=> "--- \n- 1\n- 2\n- 3\n"

How can I get inline list so that output of to_yaml will be
=> "--- [1, 2, 3]"
 
W

why the lucky stiff

require 'yaml'
l = [1, 2, 3]
l.to_yaml
=> "--- \n- 1\n- 2\n- 3\n"

How can I get inline list so that output of to_yaml will be
=> "--- [1, 2, 3]"
The YAML library which comes with Ruby 1.8.2 won't allow this. But
current versions of Syck CVS simply require you to add a `to_yaml_style'
method:
l = [1, 2, 3] => [1, 2, 3]
def l.to_yaml_style; :inline; end => nil
puts l.to_yaml
--- [1, 2, 3]

_why
 
M

meruby

Thanks, I was trying this with latest version of syck instead of CVS. I
have another question:
How can you do this for each items in Hash? for example

=>d = {'first', [1,2,3], 'second', [11,12,13]}
=>d.to_yaml
=> "--- \nsecond: \n- 11\n- 12\n- 13\nfirst: \n- 1\n- 2\n- 3\n"
irb(main):011:0> def d.to_yaml_style; :inline; end
=> nil
irb(main):013:0> d.to_yaml
=> "--- {second: [11, 12, 13], first: [1, 2, 3]}\n"


Instead I want
"--- {second: [11, 12, 13], \n first: [1, 2, 3]}\n"
so that each key/value are inline but each of them are on seperate
line? In short I am trying to reduce # of lines in yaml but keep it
human readable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top