[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to output inline list by to_yaml

meruby

5/14/2005 7:56:00 PM

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]"

2 Answers

why the lucky stiff

5/14/2005 9:49:00 PM

0

meruby@gmail.com wrote:

>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


meruby

5/14/2005 10:40:00 PM

0

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.