[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

super behavior different in store and []= not in Hash subclass

billmcn

6/10/2006 12:43: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 []= functions. 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

1 Answer

Tim Hunter

6/10/2006 1:07: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 []= functions. 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
>

You can't use "super" like you're trying to do. "super" is not a
reference to the parent class, it's a keyword that allows you to call
the method of the same name in the parent. You can call it with an
argument list or with no argument list, in which case it uses the
current argument list.

def []=(item, value)
super # calls super(item, value) in the parent
end

If you want to call #store in the parent, just code

def []=(item, value)
store(item, value)
end

Unless of course you've defined #store in the current class, in which
case you'll have to do this:

def store(item, value)
super
end

def []=(item, value)
store(item, value)
end

There are, I'm sure, various tricky ways of finessing this that can be
explained by better Rubyists than I, but this is the simple explanation.

Search for "super" here: http://www.ruby-doc.org/docs/Progra...