super behavior different in store and []= not 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 []= 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
     
    billmcn, Jun 10, 2006
    #1
    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,793
    Darryl Pierce
    Feb 24, 2005
  2. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  3. rp
    Replies:
    1
    Views:
    539
    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:
    126
    Hugh Sasse Staff Elec Eng
    Dec 11, 2003
  5. billmcn
    Replies:
    2
    Views:
    83
    billmcn
    Jun 13, 2006
Loading...

Share This Page