3

I'm trying to understand the serialization/deserialization of ruby object using YAML under 1.8.7 and 1.9+ and have a few queries regarding it (I can't find much documentation on this)

Ruby 1.8.7

class Element
    yaml_as "tag:ruby.yaml.org,2002:Element"

    def self.yaml_new(klass, tag, val)
      puts "print you are in yaml"
    end
  end


  YAML.parser
  => syck     ## parser is set as syck all good     
   e =  Element.new.to_yaml
   => "--- !ruby/Element {}\n\n" 

Question 1: Now why didn't I get tag:ruby.yaml.org,2002:Element instead of --- !ruby/Element {}\n\n ?

  YAML.load(e) 
  =>  print your are in "yaml" ##print statement execute all good 

  ## Not sure who to change the parser in 1.8 to `pysch` so skipping this part

Ruby 1.9+

  YAML::ENGINE.yamler
   => pysch 
   e =  Element.new.to_yaml  
   => "--- !<tag:ruby.yaml.org,2002:Element> {}\n"  ## I see the tag in 1.9+ fine 

Question 2 : Why is it working in 1.9+ and not in 1.8.7 ?

   YAML.load(e)
   =>    ## The print is not getting printed 

Question 3: Why print you are in yaml isn't getting printed? In other words, why is self.yaml_new not getting invoked on YAML.load? Is it not supported under 1.9+ ?

   YAML::ENGINE.yamler = 'syck'
   => syck
   u =  Element.new.to_yaml
   => "--- !ruby/object:Element {}\n\n" 

Question 4 (similar to question #1) : Why is the the tag(tag:ruby.yaml.org,2002:Element) missing in the serialize yaml above

   YAML.load(e)
   =>  ## Suprisingly, print is not executed 

Question 5 (similar to question #2): Why isn't self.yaml_new getting executed for syck and pysch parser in ruby 1.9+ ?

Can anyone can help me resolve these questions?

Ratatouille
  • 1,372
  • 5
  • 23
  • 50
  • If I were to guess on the executions, it's probably not happening for security reasons. YAML used to be built-in for Ruby 1.8.7, but now it's a separate module under a different parser/engine. Documentation for Ruby 2.0 does not seem to be up on the rdocs, but I think yaml_new only gets called in Rails, not in Ruby – icy Aug 19 '13 at 22:47
  • 2
    I'd recommend fixing the formatting. All the bold text is like yelling. A tiny bit goes a very long way; it's painful to read, making me not want to even try. Plus, on SO, YOU GET TWO, maybe THREE questions at most so reduce what YOU WANT TO ASK! – the Tin Man Aug 20 '13 at 03:47
  • ruby 1.8.7 is [longer supported](http://www.ruby-lang.org/en/news/2013/06/30/we-retire-1-8-7/) – zhon Jan 13 '14 at 05:36
  • 1
    You have 5 questions which on light reading seem to be, "Why is there a bug in an unsupported library in an unsupported version?" I am sure this is not what you meant to ask. If you simplify your question to "Given X, how do I get Y to work?" you are more likely to get an answer. – zhon Feb 06 '14 at 14:32
  • 1
    I'm with @zhon, you should ask for the real problem, unless the questions are for curiosity sake. Anyway, old versions of Ruby YAML parsers were a mess and had more than one inconsistencies, so I'm not surprised about these weird behaviours, just look at [psych issues](https://github.com/tenderlove/psych/issues) to get an idea about their weirdness – mdesantis Feb 24 '14 at 11:57

1 Answers1

0
  • Question 1: I don't have any answer for this one
  • Question 2: Because Psych handles it correctly but you should use yaml_tag instead of yaml_tag
  • Question 3: Use def yaml_initialize(tag, val) and it will work
  • Question 4: Because it's Syck again, it behaves like in Ruby 1.8
  • Question 5: Same as Q4
Happynoff
  • 1,365
  • 1
  • 14
  • 28