Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Quick way to copy data from super class into subclass?

Reply
Thread Tools

Quick way to copy data from super class into subclass?

 
 
Audrey A Lee
Guest
Posts: n/a
 
      02-22-2010
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
 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-22-2010
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing super-class methods from super-class fi3nd Java 0 04-08-2010 03:10 PM
super.super.super how? Java 24 02-24-2005 10:51 PM
$class::SUPER as well as $instance::SUPER? Matthew Braid Perl Misc 2 08-09-2004 12:05 AM
WHICH LENS? - SIGMA 70-300mm DL MACRO Super-II or 28-300mm AF Super Zoom 4 - for Canon 300d Rebel Digital SLR? No Spam Digital Photography 24 11-27-2003 08:21 AM
Getting the super class via the super() function Fernando Rodriguez Python 2 11-22-2003 12:08 AM



Advertisments