# Constant visibility

**URL:** <https://www.ruby-forum.com/t/constant-visibility/156618>\
**Category:** Ruby\
**Created:** [December 31, 2008, 12:22am UTC](https://www.ruby-forum.com/t/constant-visibility/156618 "2008-12-31T00:22:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [December 31, 2008, 12:22am UTC](https://www.ruby-forum.com/t/constant-visibility/156618/1 "2008-12-31T00:22:20Z")

</div>

Hi \*,

module Included

def whatever  
do\_something\_with FOO  
end

end

module Includer

include Included

class Whatever  
FOO = ‘whatever’  
end

end

this is not just an example to invent stupid variable names, but a  
question.  
I would like to include the Included module and have visibility on the  
Includer module constants (think about validation, different classes  
need different data).  
Can you suggest me a way to accomplish that, to have a method inside a  
module that checks data inside a class in another module, and define  
the rules in the class itself ?  
“You’re an idiot you should do like that” answers are welcome 🙂

ngw

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [December 31, 2008, 1:31am UTC](https://www.ruby-forum.com/t/constant-visibility/156618/2 "2008-12-31T01:31:30Z")

</div>

On Tue, Dec 30, 2008 at 11:20 PM, Nicholas W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi \*,

[snip example code]

> Can you suggest me a way to accomplish that, to have a method inside a  
> module that checks data inside a class in another module, and define the  
> rules in the class itself ?

This is one approach:

module Included  
def whatever  
puts self.class::FOO  
end  
end

module Includer  
class Whatever  
include Included  
FOO = ‘whatever’  
end  
end

o = Includer::Whatever.new  
o.whatever # =\> “whatever”

Note you have to include the module inside the class - including it in  
the enclosing module does not do what you may think 🙂

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![Nicholas\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f17d59/32.png) [@Nicholas\_W](https://www.ruby-forum.com/u/Nicholas_W)\
**Post date:** [January 1, 2009, 8:22am UTC](https://www.ruby-forum.com/t/constant-visibility/156618/3 "2009-01-01T08:22:09Z")

</div>

You might be looking for extend instead of include:

> module Foo

> 

def foo

> 

```
puts self::FooBar::TEST;

```

> 

end

> 

> 

end

> 

> module Bar

> 

extend Foo;

> 

> 

class FooBar

> 

```
TEST = "Whatever";

```

> 

end

> 

> end

> Bar.foo; # Prints out “Whatever”

See if it works for you
