Bug? Hash::include not working properly

K

Kero

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 ---+
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Bug? Hash::include not working properly"

|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.
 
K

Kero

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 ---+
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top