What is YAML::Syck::Map?

Discussion in 'Ruby' started by Johannes Grassler, Aug 17, 2011.

  1. Hello,

    I've been having loads of fun trying to work out how to de-serialize
    a YAML dump of a data base I'm working with. More to the point I need to
    process the YAML dump as a tree to restore child-parent relations
    between objects (i.e. knowing the parent node when recreating a child

    Here's the relevant excerpt from my code:

    | tree = YAML.parse(raw) # raw is a string holding the YAML input
    | tree.children.each { |child|
    | restore_object(child) }

    And here's my YAML input:

    | --- !yaml.org,2002:GraphLabel
    | attributes:
    | network_elements:
    | - !yaml.org,2002:NetworkElement
    | attributes:

    I.e. the YAML input is a tree with one '--- !yaml.org,2002:GraphLabel'
    at its root, at least if I interpret the dashes/indentation notation
    right (please correct me if I'm wrong; I've only begun to familiarize
    myself with YAML recently).

    I want to call restore_object() on all nodes of type
    !yaml.org,2002:NetworkElement. According to
    the call to YAML.parse should yield a YamlNode object that's
    effectively the root of a tree I can descend into using its children()

    This is not so, for:

    a) I do not get an object of type YamlNode but of YAML::Syck::Map

    This is very mysterious critter about which little is known:


    b) While said mysterious object does have a children() method that
    method returns nil instead of the list of
    !yaml.org,2002:NetworkElement I was expecting.

    Can anyone shed some light on the issue or, better yet provide pointers to
    comprehensive documentation?

    For <http://yaml4r.sourceforge.net> is pretty thin on parsing YAML as
    object trees, and <http://www.ruby-doc.org> is even worse (most YAML
    methods are "documented" by a method prototype and a body that consists
    of "[Source]").


    Johannes Grassler, Aug 17, 2011
    1. Advertisements

  2. I get this on ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin]:

    irb(main):015:0> s=<<XXX
    irb(main):016:0" --- !yaml.org,2002:GraphLabel
    irb(main):017:0" attributes:
    irb(main):018:0" network_elements:
    irb(main):019:0" - !yaml.org,2002:NetworkElement
    irb(main):020:0" attributes:
    irb(main):021:0" XXX
    => "--- !yaml.org,2002:GraphLabel \nattributes:\nnetwork_elements:\n-
    irb(main):022:0> x=YAML.parse s
    => #<Syck::Map:0x1081a4d8>
    irb(main):023:0> x
    => #<Syck::Map:0x1081a4d8>
    irb(main):024:0> x.children
    => [nil, nil, nil]
    You can at least click on [Source] and see it (the source).


    Kind regards

    Robert Klemme, Aug 17, 2011
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.