6.3 Constants

A loose definition

Constants in Ruby are defined by convention. In the same way instance variables come into existence when you name a variable with an "@" prefix, constants come into existence when you name a *cough* variable *cough* with an upper-case character prefix. I use the term "variable" so sloppily here because Ruby is a bit strange about constants. Try changing the value of Argument::Truth in the following exercise.

Hint

Argument::Truth can be assigned directly, believe it or not.

Output Window

Strange, no? You can't see it in the STDOUT above, but Ruby will at least complain with a warning something like this: warning: already initialized constant Truth. This works because Ruby is forgiving about assigning a constant value twice statically. Because classes and modules are constants, this is helpful if one accidentally requires the same file into a program twice.

If you try to redeclare a constant dynamically, however, Ruby will get upset:

Example Code:

Output Window

Note that one not need attempt to redefine a constant to get this error. Even if you delete the first line in the above example, it will fail with the same error. Ruby does not want you to assign (or re-assign) constants while your program is running -- and that's a good thing.

Classes and modules: less special than they look

You've probably noticed that Ruby requires you use a constant when naming classes and modules. (If not, try naming a class with a lower-case character.) There is a way around this, however, and in its use we can see that classes and modules themselves are types in the Ruby class hierarchy, just like strings and arrays.

Example Code:

Output Window

As with many of Ruby's "whoa, what? you can do that?" features, you will find this syntax most helpful when you begin metaprogramming. For now, this syntax is useful as a tool to teach about what Ruby looks like on the inside... and that, in fact, less magic is happening than would appear at first.

Try creating classes in these exercises the same way.

Output Window

Notice that we had to wrap our sheep class in a method to get access to it from our tests. Try namespacing it as you would normally in this next exercise. You'll find you run into a new issue.

Hint

You will need the `::` operator to get to the sheep's class. But `::` can only look up constants! Hmm.

Output Window

One wouldn't think there would be so much to explore in the creation of constants! But now you are armed with an understanding of how they work in your regular Ruby programs and you'll be prepared to tackle the even stranger bits once you start metaprogramming.

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Jasim A Basheer