title: Ruby YAML parser by passing constructor
body: <p>I am working on an application that takes input from a YAML file, parses them into objects, and let's them do their thing. The only problem I'm having now, is that the YAML parser seems to ignore the objects "initialize" method. I was counting on the constructor to fill in any instance variables the YAML file was lacking with defaults, as well as store some things in class variables. Here is an example:</p>
  
  <pre><code>class Test
  
      @@counter = 0
  
      def initialize(a,b)
          @a = a
          @b = b
  
          @a = 29 if @b == 3
  
          @@counter += 1
      end
  
      def self.how_many
          p @@counter
      end
  
      attr_accessor :a,:b
  
  end
  
  require 'YAML'
  
  a = Test.new(2,3)
  s = a.to_yaml
  puts s
  b = YAML::load(s)
  puts b.a
  puts b.b
  Test.how_many
  
  puts ""
  
  c = Test.new(4,4)
  c.b = 3
  t = c.to_yaml
  puts t
  d = YAML::load(t)
  puts d.a
  puts d.b
  Test.how_many
  </code></pre>
  
  <p>I would have expected the above to output:</p>
  
  <pre><code>--- !ruby/object:Test
  a: 29
  b: 3
  29
  3
  2
  
  --- !ruby/object:Test
  a: 4
  b: 3
  29
  3
  4
  </code></pre>
  
  <p>Instead I got:</p>
  
  <pre><code>--- !ruby/object:Test
  a: 29
  b: 3
  29
  3
  1
  
  --- !ruby/object:Test
  a: 4
  b: 3
  4
  3
  2
  </code></pre>
  
  <p>I don't understand how it makes these objects without using their defined initialize method. I'm also wondering if there is anyway to force the parser to use the initialize method.</p>