# Non-useless mode for YAML::Syck::Map.children\_with\_index?

**URL:** <https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109>\
**Category:** Ruby\
**Created:** [December 12, 2006, 7:01am UTC](https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109 "2006-12-12T07:01:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [December 12, 2006, 7:01am UTC](https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109/1 "2006-12-12T07:01:26Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [December 12, 2006, 3:22pm UTC](https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109/2 "2006-12-12T15:22:13Z")

</div>

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
> 
> ```
> 
> …

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [December 12, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109/3 "2006-12-12T15:52:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [December 12, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/non-useless-mode-for-yaml-map-children-with-index/79109/4 "2006-12-12T16:30:35Z")

</div>

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. 🙂

I will stop researching this now; thanks all.

– Phlip
