Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Bug? Hash::include not working properly

Reply
Thread Tools

Bug? Hash::include not working properly

 
 
Kero
Guest
Posts: n/a
 
      07-16-2005
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 ---+
 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      07-16-2005
Hi,

In message "Re: Bug? Hash::include not working properly"
on Sat, 16 Jul 2005 20:00:53 +0900, Kero <(E-Mail Removed)-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.


 
Reply With Quote
 
 
 
 
Kero
Guest
Posts: n/a
 
      07-16-2005
> 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 ---+
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yahoo group not working properly with Firefox History Fan Firefox 3 06-27-2005 02:30 AM
ICS not working properly AuthorizedUser Wireless Networking 2 08-23-2004 04:46 AM
Not sure GNU GMP is working properly...any ideas Jasper Perl 0 06-28-2004 02:20 AM
More American Graffiti: Properly Framed, Properly Scored? Scot Gardner DVD Video 0 09-02-2003 02:28 AM
Chomp not working properly with Sendmail Aaron Powell Perl 0 07-24-2003 02:24 PM



Advertisments