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

Discussion in 'Ruby' started by billmcn, Jun 10, 2006.

  1. billmcn

    billmcn Guest

    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
    billmcn, Jun 10, 2006
    #1
    1. Advertising

  2. 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
    Marcin Mielżyński, Jun 10, 2006
    #2
    1. Advertising

  3. billmcn

    billmcn Guest

    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
    billmcn, Jun 13, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,758
    Darryl Pierce
    Feb 24, 2005
  2. jstorta
    Replies:
    3
    Views:
    438
    jstorta
    Feb 20, 2006
  3. rp
    Replies:
    1
    Views:
    512
    red floyd
    Nov 10, 2011
  4. Hugh Sasse Staff Elec Eng

    Inheriting variables, super, and "not super"?

    Hugh Sasse Staff Elec Eng, Dec 9, 2003, in forum: Ruby
    Replies:
    7
    Views:
    123
    Hugh Sasse Staff Elec Eng
    Dec 11, 2003
  5. billmcn
    Replies:
    0
    Views:
    77
    billmcn
    Jun 10, 2006
Loading...

Share This Page