Class Constants in Ruby
Class Constants are useful in PHP and Ruby as a way of defining a specific piece of data that should not change a.k.a. they are constant. Class constants are especially useful in a few ways:
- Defining numbers that are static
- Defining an array of available states
Let’s pretend we have a class that we use to calculate a the total cost of goods. Very much similar to a cash register at the grocery store.
This class will allow our cashier to add items to the total, then on checkout the class will automatically calculate the sales tax.
For this example, we’re going to assume we’re shopping in my home state of Ohio which has a 5.75% sales tax:
Something worth pointing out, if you noticed we’re returning $this
in the addItem()
method. This is using Builder Pattern which creates a fluent interface.
Return $this
which is the object itself allows us to chain methods:
Also, an added bonus to using the const
method to define constant variables, you can access this sales tax value outside of the class:
Now, if another class relies on the sales tax amount, you can use that constant without defining it twice. This helps keep your code DRY and makes it more flexible to change.
Class Constants in Ruby
Ruby also supports class constants. Let’s learn by example:
class PriceCalculator
SALES_TAX_PERCENTAGE = 0.0575
attr_accessor :total_price
def initialize
@total = 0
end
def add_item(item)
@total += item
self
end
def total
@total = @total * (1 - SALES_TAX_PERCENTAGE)
// rounding the total so we get a nice 2 decimal points like 9.99
return "$#{@total.round(2)}"
end
end
It’s striking how similar the languages are in practice. Sure the syntax is a little different, but essentially under the hood, Ruby just takes advantage of the Builder Pattern at a low level.
In PHP we have a built in keyword const
that helps us define class constants. In Ruby, it’s best practice to just define an all caps variable name inside of the class and that’s it.
Even Ruby’s class constants are available outside of the class just like PHP:
puts PriceCalculator::SALES_TAX_PERCENTAGE
=> 0.0575
Ruby even uses the same double colon notation ::
to define calling a constant.
Freezing Ruby Class Constants
One minor best practice to apply with Ruby class constants are applying a .freeze
method. Ruby’s dynamic nature allows you to slice and dice classes and objects easily if you’d like. Immutability isn’t a top concern in Ruby.
So to enforce immutability on class constants which are static, call the .freeze
method on them:
class Cat
SPECIES = 'Felis catus'.freeze
end
It’s small and chances are you won’t accidentally overwrite a class constant but it’s always best to cover your bases.