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

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…

Phlip wrote:

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

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

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

Devin M. wrote:

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. :slight_smile:

I will stop researching this now; thanks all.

– Phlip