YAML and X < Hash

M

Matt Mower

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
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top