# Global constants

**URL:** <https://www.ruby-forum.com/t/global-constants/146127>\
**Category:** Ruby\
**Created:** [August 18, 2008, 10:20am UTC](https://www.ruby-forum.com/t/global-constants/146127 "2008-08-18T10:20:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [August 18, 2008, 10:20am UTC](https://www.ruby-forum.com/t/global-constants/146127/1 "2008-08-18T10:20:58Z")

</div>

I want set up some constants that can be accesed since another  
classes.

Which is the best way? Using a singleton to access to variables? (as  
if were global vars.)?

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [August 18, 2008, 11:23am UTC](https://www.ruby-forum.com/t/global-constants/146127/2 "2008-08-18T11:23:07Z")

</div>

Kless wrote:

> I want set up some constants that can be accesed since another  
> classes.
> 
> Which is the best way? Using a singleton to access to variables? (as  
> if were global vars.)?

You can use a module for this. The classes don’t necessarily have to be  
in the module either, if outside the module they can address the  
constant as MyModule::CONSTANT or the classes can include MyModule if it  
makes sense to do so.

module MyModule  
CONSTANT = “test”

class MyClass1  
def print\_test  
puts CONSTANT  
end  
end

class MyClass2  
def say\_something  
puts CONSTANT  
end  
end  
end

MyModule::MyClass1.new.print\_test  
MyModule::MyClass2.new.say\_something

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [August 18, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/global-constants/146127/3 "2008-08-18T19:05:44Z")

</div>

Thank you for your help and the fast reply.

I come from Python, a great language, but I’m loving Ruby.
