Non-useless mode for YAML::Syck::Map.children_with_index?

P

Phlip

Rubistas:

Here's a complete working sample illustrating the issue in YAML::Syck, or
its documentation:

require 'yaml'

y = YAML.parse("i: b\na: sequence")
p y.kind # <-- :seq

y.children_with_index.each do |me_nil, me_key|
p me_nil # <-- nil
p me_key # <-- a Scalar
p me_key.value # <-- i, a
p y[me_key] # <-- nil
y.value[me_key] # <-- the real child object!
end

The general goal is very simple: To walk the object model, all the way from
the root :seq down to a leaf Scalar, without ever stepping out of Syck
objects, into Hash or Array objects. The reason is because they might have
different features, so I might lose some Syck abilities. This might just be
impossible, so I need to know that before committing to any one strategy.

Incidentally, the existing implementation solves the primary mismatch
between the OMap object (which I also cannot figure out how to grab
directly) and the Hash, whose keys are in an undefined sequence. The
children_with_index returns the indices in the correct order, so the last
line y.value[key] can then get the children in the right order.

But yet children_with_index doesn't return the children...
 
D

Devin Mullins

Phlip said:
Rubistas:
Ooh! I approve.

Err... you might wanna ask _why directly, or hop into the source. I got
lost at the :seq part -- isn't that a Mapping, not a Sequence?

And FWIW, I got a, i from me_key.value when I ran it. So... order bad. I
think if you care about order -- now's too late in the game.

irb(main):021:0* y.instance_variables.map {|f| [f,
y.instance_variable_get(f)] }
=> [["@style", nil], ["@value",
{#<YAML::Syck::Scalar:0x2e0f43c>=>#<YAML::Syck::
Scalar:0x2e0f48c>,
#<YAML::Syck::Scalar:0x2e0f39c>=>#<YAML::Syck::Scalar:0x2e0f3
ec>}], ["@type_id", nil], ["@kind", :seq]]

Nothing about order there.

(If you have control over the YAML, don't use a mapping - the spec says
order doesn't matter for mappings. A technique I use a lot is a list of
singleton mappings:
- Foo: bar
- Fish: bileff
...)

Devin
 
D

Devin Mullins

Another option, if you have control over the YAML -- tag the mapping
with !ruby/object:OrderedHash, and go to town on an OrderedHash
implementation that enumerates its junk in order of assignment.

Devin the Crickety Shiv
 
P

Phlip

Devin said:
Another option, if you have control over the YAML -- tag the mapping
with !ruby/object:OrderedHash

Ooh I forgot to mention (between yesterday's saturation life-issues)
that I'm using !omap when I want an order.
Err... you might wanna ask _why directly,

I'm ever-so-slightly daunted...
or hop into the source

....and I'm very good at writing Ruby, but not so good at reading
others's. :)

I will stop researching this now; thanks all.

-- Phlip
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top