YAML and X < Hash

Discussion in 'Ruby' started by Matt Mower, Apr 10, 2005.

  1. Matt Mower

    Matt Mower Guest

    Hi folks.

    Porting some Python code to Ruby I have come across an issue in the
    YAML module. The Python code uses a Dict subclass which I translated
    into a Hash subclass in my Ruby code.

    When I use YAML to store my Hash subclass instances none of the
    instance variables are stored, only the hashed pairs. This seems to
    be the case for Hash and any subclasses of Hash, e.g.

    class Y < Hash
    attr_accessor :value

    def initialize( value )
    @value = value
    end
    end

    y = Y.new( "test" )
    puts y.to_yaml

    =>

    --- !ruby/hash:Y {}

    I converted my code to use a Hash by composition rather than
    inheritance which avoids the loss of the instance variables in the
    subclass. However the default value for the Hash is an instance
    variable and is also lost.

    Regards,

    Matt

    --
    Matt Mower :: http://matt.blogs.it/
    Matt Mower, Apr 10, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  2. RubyQuestions
    Replies:
    0
    Views:
    220
    RubyQuestions
    Dec 3, 2003
  3. Paul Battley

    YAML.dump/YAML.load bug

    Paul Battley, Aug 3, 2005, in forum: Ruby
    Replies:
    0
    Views:
    176
    Paul Battley
    Aug 3, 2005
  4. Eric Promislow
    Replies:
    4
    Views:
    206
    Eric Promislow
    Oct 31, 2006
  5. Joshua Choi

    yaml.rb and YAML "%" directives

    Joshua Choi, Jan 14, 2007, in forum: Ruby
    Replies:
    1
    Views:
    160
Loading...

Share This Page