Same class, different module

C

Caleb Tennis

Here's a small snippet of code I'm trying to hack on:

module Mod1
class A
def meth
puts "in Mod 1"
end
end

class B
def initialize
A.new.meth
end
end
end

module Mod2
class A < Mod1::A
def meth
put "in Mod 2"
end
end

class B < Mod1::B
end
end

p Mod1::B.new
p Mod2::B.new



caleb@tcdevel ~ $ ruby testit.rb
in Mod 1
#<Mod1::B:0xb7c888dc>
in Mod 1
#<Mod2::B:0xb7c88508>

What I'm trying to accomplish is having B reference A but in the same module
its currently in. I can accomplish this by redefining the initialize method
again in Mod2::B, such that it looks exactly the same as Mod1::B, but I'm
hoping to find a trick to avoid having to do that.

Any thoughts?

Caleb
 
E

Eero Saynatkari

Caleb said:
Here's a small snippet of code I'm trying to hack on:

<snip due to ruby-forum.com quote limit />

What I'm trying to accomplish is having B reference A but in the same
module
its currently in. I can accomplish this by redefining the initialize
method
again in Mod2::B, such that it looks exactly the same as Mod1::B, but
I'm
hoping to find a trick to avoid having to do that.

Any thoughts?

You could use #const_get, possibly, since a method call is dynamic.


E
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top