# Method defined in module doesn't have access to Constant in

**URL:** <https://www.ruby-forum.com/t/method-defined-in-module-doesnt-have-access-to-constant-in/91490>\
**Category:** Ruby\
**Created:** [April 9, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/method-defined-in-module-doesnt-have-access-to-constant-in/91490 "2007-04-09T19:38:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 9, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/method-defined-in-module-doesnt-have-access-to-constant-in/91490/1 "2007-04-09T19:38:24Z")

</div>

Does anyone know why a method defined in a module that is then included  
in a  
class does not have  
access to the constants of that class? Doesn’t including the module  
make  
all its methods into instance methods  
of the class? Shouldn’t get\_foo and class\_get\_foo in the following  
example  
work the same?

module ModFoo  
def get\_foo  
FOO  
end  
end

class Foo  
FOO = ‘this is foo’  
include ModFoo

def class\_get\_foo  
FOO  
end  
end

f = Foo.new  
f.get\_foo

> > f.get\_foo  
> > NameError: uninitialized constant ModFoo::FOO  
> > from (irb):3:in `get\_foo’  
> > from (irb):11  
> > from :0

f.class\_get\_foo  
=\> “this is foo”

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 9, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/method-defined-in-module-doesnt-have-access-to-constant-in/91490/2 "2007-04-09T20:25:29Z")

</div>

On 4/9/07, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> f.class\_get\_foo  
> =\> “this is foo”

Because Mod::FOO, and Foo::FOO are two different constants in two  
different namespaces.

Included methods aren’t source macros, they get compiled in the  
context of the module that contains them, so when ModuleFoo#getFoo was  
compiled, FOO got bound to ModuleFoo::FOO, hence the error.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [April 10, 2007, 12:33am UTC](https://www.ruby-forum.com/t/method-defined-in-module-doesnt-have-access-to-constant-in/91490/3 "2007-04-10T00:33:05Z")

</div>

Mike S. wrote:

> Does anyone know why a method defined in a module that is then included  
> in a  
> class does not have  
> access to the constants of that class? Doesn’t including the module make  
> all its methods into instance methods  
> of the class? Shouldn’t get\_foo and class\_get\_foo in the following example  
> work the same?

You can make it work as follows:

> module ModFoo  
> def get\_foo  
> FOO  
> self.class::FOO
