Hi,
In message "Re: [ruby-talk:15461] Re: more reflection"
on 01/05/21, Mathieu Bouchard <matju / sympatico.ca> writes:
|> There's no such thing like repeated inheritance (or inclusion) in
|> Ruby. Currently second attempt of inclusion is just ignored.
|Please see the program listing in [ruby-talk:12558], and:
|
| 1. Explain why that program works.
|
| 2. Explain why you think that feature is a really bug.
|
| 3. Explain why that feature should be removed.
Sorry, I don't get your point. The code is:
$n = ARGV[0].to_i
$count = 0
module Foo; def count; $count+=1; super; end end
module Bar; def count; 0; end end
class Zut
include Bar
zut = self.new
$n.times { include Foo.dup; zut.count }
end
p $count
and because of "Foo.dup", it is not repeated inclusion. it's n-times
inclusion of distinct copies of Foo. The misunderstanding is probably
caused by Foo.dup also has print name of "Foo".
So the answers would be
1. copies of Foo are included into Zut.
2. I don't think it's a bug.
3. I don't think it should be removed.
|> |also I'd like a method to find whether an object has a singleton class,
|> |without creating one. This is not possible because Object itself doesn't
|> |have an #extended_modules, though it has a #singleton_methods.
|> class <<obj
|> p self.instance_methods
|> end
|
|Not only this piece of code does not find out whether there was already a
|singleton class associated to that object... but, according to page 243 of
|the Pickaxe book, this actually creates a singleton class for the object.
|
|Which leaves this question doubly unanswered.
Oops... singleton_methods do the job for most of the cases,
unless you really want to know existance of empty singleton
class, which usually can be ignored.
matz.