[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Noobie ... Simple Inheriting from Hash Question ..

Neville Burnell

5/19/2005 1:02:00 AM

> Another option that hasn't been mentioned yet is to use delegation.
> You can have a wrapper around a hash that contains all your additional
> methods and references a Hash instance.

Hi Robert,

Could you elaborate with code pls?

Kind regards

Nev




1 Answer

Gavin Kistner

5/19/2005 3:01:00 AM

0

On May 18, 2005, at 7:01 PM, Neville Burnell wrote:
>> Another option that hasn't been mentioned yet is to use delegation.
>> You can have a wrapper around a hash that contains all your
>> additional
>> methods and references a Hash instance.
>
> Could you elaborate with code pls?

class MyHash
def initialize
@hash = {}
end

# Do something special to the hash
def square_all
@hash.each{ |k,v| @hash[k] = v*v }
end

# Pass any method I don't understand on to the hash
def method_missing(meth, *args, &block)
@hash.send(meth, *args, &block)
end
end