# Refactoring Global Constants

**URL:** <https://www.ruby-forum.com/t/refactoring-global-constants/193419>\
**Category:** Ruby\
**Created:** [August 17, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/refactoring-global-constants/193419 "2010-08-17T13:34:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [August 17, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/refactoring-global-constants/193419/1 "2010-08-17T13:34:37Z")

</div>

Hi,

I’m going to ask a dumb question (sorry for that, in advance)…

What’s the most elegant syntax and form for refactoring Global Constants  
out of Ruby code so they can be shared across different Ruby code files?

For example, I have a variable that shows up in three different files  
and looks like:

```
 maxColumns = 30

```

I’d like to know what the most elegant way is to define it in one place  
and share it, across files.

BTW, if there’s a book or document out there that specifically talks  
about different ways to refactor Ruby Code, I’d appreciate a pointer to  
it.

Thanks for your help,

FG

---

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [August 17, 2010, 2:35pm UTC](https://www.ruby-forum.com/t/refactoring-global-constants/193419/2 "2010-08-17T14:35:00Z")

</div>

Frank G. wrote:

> For example, I have a variable that shows up in three different files  
> and looks like:
> 
> ```
> maxColumns = 30
> 
> ```
> 
> I’d like to know what the most elegant way is to define it in one place  
> and share it, across files.

The best way to define a constant is using a constant 🙂

MAX\_COLUMNS = 30

Put this in a source file (e.g. limits.rb) then require ‘limits’ where  
you need it.

For any code which might be re-used or shared, you’d be better keeping  
your constants in their own namespace:

module MyStuff  
MAX\_COLUMNS = 30  
end

This can be referenced as MyStuff::MAX\_COLUMNS, but usually your code  
would also be inside the same module, so it can just use MAX\_COLUMNS.

If your code consists mainly of one class, then just put the constants  
inside that class (remember that a Class is also a Module)

class Foo  
MAX\_COLUMNS = 30  
end

…

require ‘foo/constants’  
class Foo  
def initialize(n)  
raise ArgumentError, “Too many columns” if n \> MAX\_COLUMNS  
@cols = n  
end  
end

If this value is not actually a constant, but might change at runtime  
(e.g. from a command-line option), then you could use a  
$global\_variable, but I’d suggest a class instance variable.

class Foo  
@max\_columns = 30 # default value  
def self.max\_columns  
@max\_columns  
end  
def self.max\_columns=(n)  
@max\_columns = n  
end  
end

…

Foo.max\_columns = Integer(ARGV[0]) if ARGV[0]  
puts Foo.max\_columns

Note that class Foo is itself an object (of class Class), and so  
@max\_columns is an instance variable of the class itself.

---

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [August 18, 2010, 2:22am UTC](https://www.ruby-forum.com/t/refactoring-global-constants/193419/3 "2010-08-18T02:22:01Z")

</div>

Hi Brian,

This is perfect. Thank you very much!

Frank
