Freeze Your Constants in Ruby

Patrick Brown
3 min readJan 12, 2019
Close-up photograph of a ruby gemstone
Photo by Joshua Fuller on Unsplash

Years ago, I worked on a very large Ruby on Rails codebase that used constants to hold lists of credit card transaction states. For example:

class Txn
ACTIONABLE_STATES
= [:authenticated, :to_settle]
DONE_STATES = [:settled, :declined]
end

However, we had a bug where a settled transaction would return true when txn.state.in? ACTIONABLE_STATES was called on it.

--

--