extend(Module) and inheritance

P

Patrick Gundlach

Hi out there,

I would like to extend an object with a module. That is no problem so
far, but I have an Class hierarchy where each module is split like
this:

--------------------------------------------------
class A
def bar; "bar" ; end

module WithDash
def bar ; "-bar-" ; end
end
end

class B < A
def foo ; "foo"; end

module WithDash
def foo ; "-foo-" ; end
end
end
--------------------------------------------------

My goal is to extend an object of B with module B::WithDash so that
the object is automatically extended with module A::WithDash as well.
Or any equivalent. Perhaps I should use something like

(x.class.ancestors - [Object,Kernel]).each { |klass|
x.extend(klass::WithDash)
}
??

With the definition of A and B above, I'd like to use something
similar to the first "extend" in this example and have both methods
use the WithDash variant:
--------------------------------------------------
b = B.new

b.extend(B::WithDash)
p b.foo # should be both with a dash
p b.bar # (this one, of course, isn't)

# this will do, but I don't want to know the superclasses of B
b.extend(A::WithDash)
p b.foo # print both with a dash
p b.bar #
 
N

nobu.nokada

Hi,

At Sun, 12 Jun 2005 00:05:52 +0900,
Patrick Gundlach wrote in [ruby-talk:145161]:
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top