# Same class, different module

**URL:** <https://www.ruby-forum.com/t/same-class-different-module/50289>\
**Category:** Ruby\
**Created:** [December 14, 2005, 10:05pm UTC](https://www.ruby-forum.com/t/same-class-different-module/50289 "2005-12-14T22:05:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@Caleb\_T](https://www.ruby-forum.com/u/Caleb_T)\
**Post date:** [December 14, 2005, 10:05pm UTC](https://www.ruby-forum.com/t/same-class-different-module/50289/1 "2005-12-14T22:05:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caleb\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@Caleb\_T](https://www.ruby-forum.com/u/Caleb_T)\
**Post date:** [December 14, 2005, 10:22pm UTC](https://www.ruby-forum.com/t/same-class-different-module/50289/2 "2005-12-14T22:22:38Z")

</div>

Caleb T. wrote:

> Here’s a small snippet of code I’m trying to hack on:
> 
> 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.

> Caleb

E

---

<div class="post-metadata">

**Author:** ![Caleb\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@Caleb\_T](https://www.ruby-forum.com/u/Caleb_T)\
**Post date:** [December 15, 2005, 11:58pm UTC](https://www.ruby-forum.com/t/same-class-different-module/50289/3 "2005-12-15T23:58:36Z")

</div>

module M1  
class A; def foo; “foo”; end; end  
end

module M2  
A = M1::A  
class A; def bar; “bar”; end; end  
end

a = M2::A

a.foo #=\> “foo”  
a.bar #=\> “bar”

Is that what you meant?

However, note that:

M2::A == M1::A

“Caleb T.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…
