# Extending class from other module

**URL:** <https://www.ruby-forum.com/t/extending-class-from-other-module/66790>\
**Category:** Ruby\
**Created:** [August 29, 2006, 11:11am UTC](https://www.ruby-forum.com/t/extending-class-from-other-module/66790 "2006-08-29T11:11:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [August 29, 2006, 11:11am UTC](https://www.ruby-forum.com/t/extending-class-from-other-module/66790/1 "2006-08-29T11:11:48Z")

</div>

1st question: If I got it right, modules in Ruby can be used as  
namespaces. Is it ok? Is there some other (preffered) way?

2nd question: I need to extend some class from one module in another  
one. Basically I do something like this:

module Mod1  
class A1  
end

class B2 \< Mod2::B1  
end  
end

module Mod2  
class B1  
end  
end

But this doesn’t really work (seems like B1 isn’t really found). How can  
I do such a thing?

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [August 29, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/extending-class-from-other-module/66790/2 "2006-08-29T16:47:30Z")

</div>

Alexander R. wrote:

> 1st question: If I got it right, modules in Ruby can be used as  
> namespaces. Is it ok? Is there some other (preffered) way?

That is the way to do it.

> 2nd question: I need to extend some class from one module in another  
> one. Basically I do something like this:
> 
> module Mod1  
> class A1  
> end
> 
> class B2 \< Mod2::B1  
> end  
> end
> 
> module Mod2  
> class B1  
> end  
> end
> 
> But this doesn’t really work (seems like B1 isn’t really found). How can  
> I do such a thing?

You need to flip those around. You cannot inherit from  
Mod2::B1 before it is defined.

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [August 29, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/extending-class-from-other-module/66790/3 "2006-08-29T16:57:27Z")

</div>

module Mod1  
class A1  
end  
end  
module Mod2  
class B1  
end  
end  
class Mod1::B2 \< Mod2::B1  
end

---

<div class="post-metadata">

**Author:** ![subzero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@subzero](https://www.ruby-forum.com/u/subzero)\
**Post date:** [August 29, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/extending-class-from-other-module/66790/4 "2006-08-29T17:01:28Z")

</div>

On 8/29/06, Alexander R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1st question: If I got it right, modules in Ruby can be used as  
> namespaces. Is it ok? Is there some other (preffered) way?

Exactly. Look for example at test/unit[1], or rails[2] as they contain  
a lot of classes divided into modules for namespace separation.

> module Mod2  
> class B1  
> end  
> end
> 
> But this doesn’t really work (seems like B1 isn’t really found). How can  
> I do such a thing?

The problem is in the order of evaluation. Install rcov (gem install  
rcov) and see for yourself what lines ruby has read already and what  
not.

I guess the problem is that B1 is not known to ruby yet. You can fix  
that by reversing the order, or just declaring the class without any  
content:

module Mod2  
class B1  
end  
end

module Mod1  
class B2 \< Mod2::B1  
def xxx…  
…  
end  
end  
end

module Mod2  
class B1  
def yyy  
…  
end  
end  
end

I don’t have ruby installed now, so I can’t verify these things, so  
please take it as such.

[1] [http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/index.html)  
[2] [http://api.rubyonrails.com/](http://api.rubyonrails.com/)
