# Modules as namespace

**URL:** <https://rubytalk.org/t/modules-as-namespace/55284>\
**Category:** ruby-talk\
**Created:** [7 September 2009 17:01 UTC](https://rubytalk.org/t/modules-as-namespace/55284 "2009-09-07T17:01:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim3](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Jim3](https://rubytalk.org/u/Jim3)\
**Post date:** [7 September 2009 17:01 UTC](https://rubytalk.org/t/modules-as-namespace/55284/1 "2009-09-07T17:01:26Z")

</div>

i am writing plugins for sketchup and use a module to encapsulate and  
protect my variable and methods from name clashes.

Is there any difference between the following?

module A  
&nbsp;&nbsp;def A.say\_hi # or self.say  
&nbsp;&nbsp;&nbsp;&nbsp;puts "hi"  
&nbsp;&nbsp;end  
end

module B; end  
def B.say\_hi  
&nbsp;&nbsp;puts "hi"  
end

thanks.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 September 2009 17:16 UTC](https://rubytalk.org/t/modules-as-namespace/55284/2 "2009-09-07T17:16:37Z")

</div>

Hi --

> i am writing plugins for sketchup and use a module to encapsulate and  
> protect my variable and methods from name clashes.
> 
> Is there any difference between the following?
> 
> module A  
> def A.say\_hi # or self.say  
> &nbsp;&nbsp;&nbsp;puts "hi"  
> end  
> end
> 
> module B; end  
> def B.say\_hi  
> puts "hi"  
> end

The only difference I'm aware of has to do with constant resolution:

&nbsp;&nbsp;&nbsp;H = "Hi from top level"

&nbsp;&nbsp;&nbsp;module A  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H = "Hi from A"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def A.say\_hi  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts H  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;module B  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H = "Hi from B"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def B.say\_hi  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts H  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;A.say\_hi # Hi from A  
&nbsp;&nbsp;&nbsp;B.say\_hi # Hi from top level

David

> **···**
>
> On Tue, 8 Sep 2009, Jim wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC / [http://www.rubypal.com](http://www.rubypal.com)  
> Ruby/Rails training, mentoring, consulting, code-review  
> Latest book: The Well-Grounded Rubyist ([http://www.manning.com/black2\](http://www.manning.com/black2%5C))
> 
> September Ruby training in NJ has been POSTPONED. Details to follow.

---

<div class="post-metadata">

**Author:** ![Jim3](https://avatars.discourse-cdn.com/v4/letter/j/f05b48/32.png) [@Jim3](https://rubytalk.org/u/Jim3)\
**Post date:** [8 September 2009 18:25 UTC](https://rubytalk.org/t/modules-as-namespace/55284/3 "2009-09-08T18:25:13Z")

</div>

Thank you, David. I wasn't even thinking about constants, but that's  
good to know.
