# Quick way to copy data from super class into subclass?

**URL:** <https://www.ruby-forum.com/t/quick-way-to-copy-data-from-super-class-into-subclass/183937>\
**Category:** Ruby\
**Created:** [February 22, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/quick-way-to-copy-data-from-super-class-into-subclass/183937 "2010-02-22T20:40:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![audreylee88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecae2f/32.png) [@audreylee88](https://www.ruby-forum.com/u/audreylee88)\
**Post date:** [February 22, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/quick-way-to-copy-data-from-super-class-into-subclass/183937/1 "2010-02-22T20:40:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audreylee88](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ecae2f/32.png) [@audreylee88](https://www.ruby-forum.com/u/audreylee88)\
**Post date:** [February 22, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/quick-way-to-copy-data-from-super-class-into-subclass/183937/2 "2010-02-22T20:50:19Z")

</div>

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
