Quick way to copy data from super class into subclass?

A

Audrey A Lee

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top