[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Different super() behavior for store() and =[]() in Hash subclass

billmcn

6/10/2006 12:54:00 AM

I created a simple class that subclasses the Hash item assignment. The
super function does differernt things depending on whether I use the
store or []= function. For example, this code works

class GoodHashSubclass < Hash
def store(item, value)
super.store(item, value)
end
end

good = GoodHashSubclass.new
good["dog"] = 5
puts good.inspect

It prints {"dog"=>5} as I would expect.

This code--which differs only in that I use []= instead of store--does
not work.

class BadHashSubclass < Hash
def []=(item, value)
super.store(item, value)
end
end

bad = BadHashSubclass.new
bad["dog"] = 5
puts bad.inspect

The BadHashSubclass code generates the following error

./subhash.rb:11:in `[]=': undefined method `store' for 5:Fixnum
(NoMethodError)

For some reason super is finding the superclass of the value 5 rather
than the superclass of self. This looks wrong to me. Is this the
expected behavior?

Thanks.
--wpm

2 Answers

Marcin Mielzynski

6/10/2006 10:35:00 AM

0

billmcn wrote:
> I created a simple class that subclasses the Hash item assignment. The
> super function does differernt things depending on whether I use the
> store or []= function. For example, this code works
>
> class GoodHashSubclass < Hash
> def store(item, value)
> super.store(item, value)
> end
> end
>
> good = GoodHashSubclass.new
> good["dog"] = 5
> puts good.inspect
>
> It prints {"dog"=>5} as I would expect.
>
> This code--which differs only in that I use []= instead of store--does
> not work.
>
> class BadHashSubclass < Hash
> def []=(item, value)
> super.store(item, value)
> end
> end
>
> bad = BadHashSubclass.new
> bad["dog"] = 5
> puts bad.inspect
>
> The BadHashSubclass code generates the following error
>
> ./subhash.rb:11:in `[]=': undefined method `store' for 5:Fixnum
> (NoMethodError)
>

You will get the same error trying:

super.[]=(item, value)


The reason is that without parameters super calls superclass method with
the same name passing all parameters by default. Store and []= methods
(they are the same, see in hash.c) return value given as a parameter (in
your case 5)

lopex

billmcn

6/12/2006 11:55:00 PM

0

That makes sense. Thanks.
Marcin Mielzynski wrote:
> billmcn wrote:
> > I created a simple class that subclasses the Hash item assignment. The
> > super function does differernt things depending on whether I use the
> > store or []= function. For example, this code works
> >
> > class GoodHashSubclass < Hash
> > def store(item, value)
> > super.store(item, value)
> > end
> > end
> >
> > good = GoodHashSubclass.new
> > good["dog"] = 5
> > puts good.inspect
> >
> > It prints {"dog"=>5} as I would expect.
> >
> > This code--which differs only in that I use []= instead of store--does
> > not work.
> >
> > class BadHashSubclass < Hash
> > def []=(item, value)
> > super.store(item, value)
> > end
> > end
> >
> > bad = BadHashSubclass.new
> > bad["dog"] = 5
> > puts bad.inspect
> >
> > The BadHashSubclass code generates the following error
> >
> > ./subhash.rb:11:in `[]=': undefined method `store' for 5:Fixnum
> > (NoMethodError)
> >
>
> You will get the same error trying:
>
> super.[]=(item, value)
>
>
> The reason is that without parameters super calls superclass method with
> the same name passing all parameters by default. Store and []= methods
> (they are the same, see in hash.c) return value given as a parameter (in
> your case 5)
>
> lopex