# How to implement class constants?

**URL:** <https://www.ruby-forum.com/t/how-to-implement-class-constants/129403>\
**Category:** Ruby\
**Created:** [February 18, 2008, 9:42am UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403 "2008-02-18T09:42:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 9:42am UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/1 "2008-02-18T09:42:45Z")

</div>

hi list,

what construct do I have to use in Ruby to create class constants (so  
no instance necessary to use it)?

thanks, Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 11:01am UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/2 "2008-02-18T11:01:22Z")

</div>

ruud wrote:

> hi list,
> 
> what construct do I have to use in Ruby to create class constants (so  
> no instance necessary to use it)?
> 
> thanks, Ruud

This seems to work:

class Dog  
Color = ‘brown’

def Dog.Color  
Color  
end

end

puts Dog.Color

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/3 "2008-02-18T12:57:09Z")

</div>

> This seems to work:

Or maybe:

class Dog  
Color = ‘brown’  
end

puts Dog::Color

Dog::Attrib = ‘cute’

puts Dog::Attrib

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/4 "2008-02-18T13:30:45Z")

</div>

Thank you both for you answer. I like the Class::var solution best;  
the other one includes more typing.

I am fairly new to Ruby so all the time I am occupied asking myself  
easy-to-answer questions. This really helps.

Another easy to answer question is a bit related:  
Most of the time I find a class.var notation of a class::var notation.  
In documentation I find class#method notation. How are these three  
related to each other?

thanks, Ruud

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/5 "2008-02-18T13:42:04Z")

</div>

ruud wrote:

> Most of the time I find a class.var notation of a class::var notation.

That’s not entirely accurate. There is no class.var. There’s  
object.method  
(where object can be a class and the method can have the same name as a  
variable, but it still has to be a method) and there’s  
namespace::Constant  
where namespace can be a class or a module and Constant has to be a  
constant  
(it can’t be any other sort of variable).  
The :: is for accessing constants in a namespace and the . is for  
calling  
methods on an object.  
It is not possible to access non-constant variables from outside the  
class  
without using methods designed for that purpose.

> In documentation I find class#method notation. How are these three  
> related to each other?

class#method describes an instance method (as opposed to class.method  
which  
would describe a class method). If you see something like String#length,  
that  
means that you could e.g. write “hello”.length, but not String.length.  
If you  
see String.new, you have to actually type String.new and not something  
like “hello”.new.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![ruud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@ruud](https://www.ruby-forum.com/u/ruud)\
**Post date:** [February 18, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/how-to-implement-class-constants/129403/6 "2008-02-18T13:50:31Z")

</div>

On 18/02/2008, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Sebastian,  
it couldn’t have been clearer. Thank you for your extensive reply!

Ruud
