Bug? Hash::include not working properly

Discussion in 'Ruby' started by Kero, Jul 16, 2005.

  1. Kero

    Kero Guest

    Hi all,

    The behaviour of both ruby 1.6 and 1.8 seems broken.
    Foo nicely overrides #initialize and #inspect when the module is
    included, but Hash does not. Feels like premature optimization to me...

    1.8 at least behaves properly when overriding Hash#initialize once more.

    Note that the 'discarding old initialize' is on line 22, not line 12.

    $ ruby1.6 -v hash_include.rb
    ruby 1.6.8 (2003-07-09) [i386-linux]
    {}
    InsertInit / II: #<Foo:0xb7fd90cc>
    II: #<Foo:0xb7fd90cc>
    hash_include.rb:22: warning: method redefined; discarding old initialize
    hash_include.rb:23:in `initialize': superclass method `initialize' disabled
    (NameError)
    from hash_include.rb:26:in `new'
    from hash_include.rb:26
    $ ruby1.8 -v hash_include.rb
    ruby 1.8.3 (2005-06-23) [i486-linux]
    {}
    InsertInit / II: #<Foo:0xb7fd6674>
    II: #<Foo:0xb7fd6674>
    hash_include.rb:22: warning: method redefined; discarding old initialize
    InsertInit / {}
    $ cat hash_include.rb
    module InsertInit
    def initialize(*args)
    super()
    puts "InsertInit / #{self.inspect}"
    end
    def inspect()
    "II: #{super}"
    end
    end

    class Hash
    include InsertInit
    end
    p Hash.new() # {} does not call #initialize at all (only ::allocate?)

    class Foo
    include InsertInit
    end
    p Foo.new("hello")

    class Hash
    def initialize(*args)
    super(*args)
    end
    end
    Hash.new()


    +--- Kero ------------------------- kero@chello@nl ---+
    | all the meaningless and empty words I spoke |
    | Promises -- The Cranberries |
    +--- M38c --- http://members.chello.nl/k.vangelder ---+
     
    Kero, Jul 16, 2005
    #1
    1. Advertising

  2. Hi,

    In message "Re: Bug? Hash::include not working properly"
    on Sat, 16 Jul 2005 20:00:53 +0900, Kero <-dot.nl> writes:

    |The behaviour of both ruby 1.6 and 1.8 seems broken.
    |Foo nicely overrides #initialize and #inspect when the module is
    |included, but Hash does not. Feels like premature optimization to me...
    |
    |1.8 at least behaves properly when overriding Hash#initialize once more.

    You are assuming Hash#initialize calls super in it, but it doesn't.
    It's not guaranteed that initialize defined in included modules to be
    called.

    |class Foo
    | include InsertInit
    |end
    |p Foo.new("hello")

    Foo does not have its own implementation of initialize, whereas Hash
    does. Try

    class Foo
    def initialize(*args)
    end
    include InsertInit
    end
    p Foo.new("hello")

    to understand the situation. Note that the priority of methods
    defined in the included modules are lower than methods defined in
    the target class.

    matz.
     
    Yukihiro Matsumoto, Jul 16, 2005
    #2
    1. Advertising

  3. Kero

    Kero Guest

    > Note that the priority of methods
    > defined in the included modules are lower than methods defined in
    > the target class.


    woops :)
    that is why #ancestors makes sense (and override always works)
    thanks!

    +--- Kero ------------------------- kero@chello@nl ---+
    | all the meaningless and empty words I spoke |
    | Promises -- The Cranberries |
    +--- M38c --- http://members.chello.nl/k.vangelder ---+
     
    Kero, Jul 16, 2005
    #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. Andreas Bogenberger
    Replies:
    3
    Views:
    960
    Andreas Bogenberger
    Feb 22, 2008
  2. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  3. Eyal Oren

    Bug in Hash#hash? (Ruby 1.8.4)

    Eyal Oren, Sep 20, 2006, in forum: Ruby
    Replies:
    6
    Views:
    128
    Rick DeNatale
    Sep 21, 2006
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    640
    David A. Black
    Jul 2, 2008
  5. PeterSShenkin
    Replies:
    9
    Views:
    109
    PeterSShenkin
    Aug 6, 2006
Loading...

Share This Page