Quick way to copy data from super class into subclass?

Hello Ruby people,

I want to create a subclass of Hash.

I want to call it MyHash.

I know how to do that.

Next I want to create objects from MyHash like:
@my_hash= MyHash.new

So, I know how to do that.

I need help with transferring data from an ordinary hash-object into
@my_hash

Suppose I have this:
@your_hash= {:k1 => 1, :k2 => 2}

Is there a quick way or idiom to copy the data from @your_hash into
@my_hash?

I tried this:

ruby-1.8.7-p174 > @your_hash= {:k1 => 1, :k2 => 2}
=> {:k2=>2, :k1=>1}

ruby-1.8.7-p174 > class MyHash < Hash ; end
=> nil

ruby-1.8.7-p174 > @my_hash= MyHash.new @your_hash
=> {}

ruby-1.8.7-p174 > @my_hash
=> {}

–Audrey

Audrey A Lee wrote:

Is there a quick way or idiom to copy the data from @your_hash into
@my_hash?

Use Hash#update:

class MyHash < Hash; end
=> nil

h = {1=>2}
=> {1=>2}

mh = MyHash.new
=> {}

mh.update h
=> {1=>2}

mh.class
=> MyHash

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs