You could just extend the hash class, rather than inheriting from it.
That is, instead of this:
class myHash < Hash
def foo
...
end
end
Do this:
class Hash
def foo
...
end
end
Then, all your hash objects will be given your 'foo' method and you can
do things like this:
{:key => "value"}.foo
David
Neville Burnell wrote:
> Hi,
>
> I have a class inheriting from Hash which has some specific methods
> operating on the hash elements:
>
> Class MyHash < Hash
>
> def foo
> ...
> end
>
> End
>
> ....
>
> Now, with Hash class, its really easy to create a new hash, eg
>
> h = {:key1 => "val1", :key2 => "val2"}
>
> What I would like to do is create a new instance of the class with the
> same simplicity, but I'd like to avoid creating a redundant Hash and
> tranferring the contents one by one to MyHash which happens if I code:
>
> h = MyHash.new(:key1 => "val1", :key2 => "val2")
>
> and then define initialize(h={})
>
> Whats the "ruby way" tm for something like this?
>
> Thanks
>
> Nev
>
>
>
>
>
>
>
>
>
>
>
>
>
>
--
David Mitchell
Software Engineer
Telogis
NOTICE:
This message (including any attachments) contains CONFIDENTIAL
INFORMATION intended for a specific individual and purpose, and
is protected by law. If you are not the intended recipient,
you should delete this message and are hereby notified that any
disclosure, copying, or distribution of this message, or the
taking of any action based on it, is strictly prohibited.