# Re: Help with object scope

**URL:** <https://www.ruby-forum.com/t/re-help-with-object-scope/74175>\
**Category:** Ruby\
**Created:** [December 21, 2005, 7:17am UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175 "2005-12-21T07:17:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 21, 2005, 7:17am UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175/1 "2005-12-21T07:17:29Z")

</div>

module M  
class C  
def self.blah  
end  
end  
end

module M2  
include M  
class TestM  
def setup; C.blah; end  
end  
end

Won’t work because the C tries to refer to TestM::C, which doesn’t  
exist.

Previously, when you did an “include M” you included the module into  
Kernel.

Since:  
TestM is an instance of Class  
Class is an instance of Object  
You’ve included M in Object, causing Object::C to be created

So, in that case TestM::C exists.

> I had a file, in short:  
> end
> 
> Works great.
> 
> _However_, if I wrap the unit tests in their own module, then  
> ruby can  
> no longer find class C. Why? Didn’t I include module M? I’m really  
> confused here.
> 
> If you want the full source, I can post it.  
> #####################################################################################  
> This email has been scanned by MailMarshal, an email content filter.  
> #####################################################################################  
> #####################################################################################  
> This e-mail message has been scanned for Viruses and Content and cleared  
> by NetIQ MailMarshal  
> #####################################################################################

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 21, 2005, 3:22pm UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175/2 "2005-12-21T15:22:25Z")

</div>

I see.

So,

1. How do I call C from class TestM without explicitly using its  
namespace (M:C)? Is there no way? I don’t want to modify Kernel or  
anything, just have class C available.

2. Could you explain exactly what a Module is? A namespace? A set of  
methods without a home? I’m really confused… (I know that  
module\_define and class\_define are synonomous, which is even more  
confusing…)

Daniel S. wrote:

> module M  
> class C  
> def self.blah  
> end  
> end  
> end
> 
> module M2  
> include M  
> class TestM  
> def setup; C.blah; end  
> end  
> end
> 
> Won’t work because the C tries to refer to TestM::C, which doesn’t  
> exist.
> 
> Previously, when you did an “include M” you included the module into  
> Kernel.
> 
> Since:  
> TestM is an instance of Class  
> Class is an instance of Object  
> You’ve included M in Object, causing Object::C to be created
> 
> So, in that case TestM::C exists.
> 
> > I had a file, in short:  
> > end
> > 
> > Works great.
> > 
> > _However_, if I wrap the unit tests in their own module, then  
> > ruby can  
> > no longer find class C. Why? Didn’t I include module M? I’m really  
> > confused here.
> > 
> > If you want the full source, I can post it.  
> > #####################################################################################  
> > This email has been scanned by MailMarshal, an email content filter.  
> > #####################################################################################  
> > #####################################################################################  
> > This e-mail message has been scanned for Viruses and Content and cleared  
> > by NetIQ MailMarshal  
> > #####################################################################################

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 21, 2005, 3:49pm UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175/3 "2005-12-21T15:49:03Z")

</div>

List R. wrote:

> 1. Could you explain exactly what a Module is? A namespace? A set of  
> methods without a home? I’m really confused… (I know that  
> module\_define and class\_define are synonomous, which is even more  
> confusing…)

A little bit of both, actually. Here’s a good description of what a  
Module is: [http://www.rubycentral.com/book/tut\_modules.html](http://www.rubycentral.com/book/tut_modules.html)

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 21, 2005, 4:25pm UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175/4 "2005-12-21T16:25:28Z")

</div>

List R. wrote:

> So,
> 
> 1. How do I call C from class TestM without explicitly using its  
> namespace (M:C)? Is there no way? I don’t want to modify Kernel or  
> anything, just have class C available.

Try this instead:

> class TestM  
> include M  
> def setup; C.blah; end  
> end

A module is a set of instance methods and constants. It can either be  
used as a namespace or mix-in functionality. (For the latter you are  
collecting similar methods / constants together in one Module so you can  
add them to your own classes or objects easily.)

mod\_or\_cls.include(mod) will add all constants and instance methods of  
mod to mod\_or\_cls.

obj.extend(mod) will add all constants and instance methods of mod to an  
object itself.

Here’s a sample:

module HalfString  
def half()  
self[0, length / 2]  
end  
end

str = “foobar”  
str.half() # raises NoMethodError  
str.extend(HalfString)  
str.half() # =\> “foo”

“hello”.half() # raises NoMethodError

You can’t ever include classes. You can only inherit from them.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [December 22, 2005, 5:46am UTC](https://www.ruby-forum.com/t/re-help-with-object-scope/74175/5 "2005-12-22T05:46:50Z")

</div>

> Try this instead:
> 
> > class TestM  
> > include M  
> > def setup; C.blah; end  
> > end

I see.

So, that’s the equivalent of:

class TestM  
class C; … ; end  
def setup; C.blah ; end  
end

What exactly does it mean to nest a class?  
I know that you cannot define a class within a method (not sure why)?

Also, is require the same thing as “copy and paste dynamically”, or does  
it drop down to the global namespace, or do something else?
