[YAML] what happened to :SortKeys option to #to_yaml?

  • Thread starter Joel VanderWerf
  • Start date
J

Joel VanderWerf

I think this used to produce sorted output in 1.8.2:

irb(main):002:0> h = {}
=> {}
irb(main):003:0> 10.times {|i| h = i}
=> 10
irb(main):004:0> puts h.to_yaml:)SortKeys => true)
---
5: 5
0: 0
6: 6
1: 1
7: 7
2: 2
8: 8
3: 3
9: 9
4: 4
=> nil

Is there still a way to do this?
 
A

ara.t.howard

I think this used to produce sorted output in 1.8.2:

irb(main):002:0> h = {}
=> {}
irb(main):003:0> 10.times {|i| h = i}
=> 10
irb(main):004:0> puts h.to_yaml:)SortKeys => true)
---
5: 5
0: 0
6: 6
1: 1
7: 7
2: 2
8: 8
3: 3
9: 9
4: 4
=> nil

Is there still a way to do this?


i just happened to be working on this today

jib:~/eg/ruby/alib/alib-0.3.1 > cat a.rb
require "alib"

oh = ALib::OrderedHash::new

("a".."z").to_a.reverse.each{|x| oh[x] = x}

oh.yaml_inline!

y oh


jib:~/eg/ruby/alib/alib-0.3.1 > RUBYLIB=./lib ruby a.rb
--- { z: z, y: y, x: x, w: w, v: v, u: u, t: t, s: s, r: r, q: q, p: p, o: o, n: n, m: m, l: l, k: k, j: j, i: i, h: h, g: g, f: f, e: e, d: d, c: c, b: b, a: a }


the class is in alib now, but the yaml_inline will break on 1.8.4. releasing soon.

cheers.


-a
 
J

Joel VanderWerf

I think this used to produce sorted output in 1.8.2:

irb(main):002:0> h = {}
=> {}
irb(main):003:0> 10.times {|i| h = i}
=> 10
irb(main):004:0> puts h.to_yaml:)SortKeys => true)
---
5: 5
0: 0
6: 6
1: 1
7: 7
2: 2
8: 8
3: 3
9: 9
4: 4
=> nil

Is there still a way to do this?


i just happened to be working on this today

jib:~/eg/ruby/alib/alib-0.3.1 > cat a.rb
require "alib"

oh = ALib::OrderedHash::new

("a".."z").to_a.reverse.each{|x| oh[x] = x}

oh.yaml_inline!

y oh


jib:~/eg/ruby/alib/alib-0.3.1 > RUBYLIB=./lib ruby a.rb
--- { z: z, y: y, x: x, w: w, v: v, u: u, t: t, s: s, r: r, q: q, p:
p, o: o, n: n, m: m, l: l, k: k, j: j, i: i, h: h, g: g, f: f, e: e, d:
d, c: c, b: b, a: a }


the class is in alib now, but the yaml_inline will break on 1.8.4.
releasing soon.

cheers.


-a


Not quite the same, is it?

I don't want to preserve the order in which I add keys. I just want the
YAML output to be sorted by key.

Also, I want the old behavior of :SortKeys, which was to do this
recursively to all hashes hanging off the object you call #to_yaml on.

This is mostly for aesthetic reasons (and being able to find keys in the
output doc).
 
A

Ara.T.Howard

Not quite the same, is it?

you're right.
I don't want to preserve the order in which I add keys. I just want the YAML
output to be sorted by key.

Also, I want the old behavior of :SortKeys, which was to do this recursively
to all hashes hanging off the object you call #to_yaml on.

This is mostly for aesthetic reasons (and being able to find keys in the
output doc).

hmmm. ifirc yaml always does this for hashes now... i may be mistaken though.

i actually hit that same bug today!

-a
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top