Loading...
X

Defining a Module on a Class from an Included Module

Today, I came across a bug in our code that was introduced when two different Ruby classes included the same module. I had a constant declared within the module and thought I had it defined such that each class would include its own copy of the constant.

It looked something like this:

Note: If you are unfamiliar with ActiveSupport::Concern, it is a Rails class that provides a slight shortcut to writing the slightly longer version of the code. It is certainly debatable whether the Rails approach buys much of an advantage. For a broader discussion about concerns, I suggest checking out this article.

I thought (incorrectly) that CrowdfundingPage::LAYOUTS would be a different hash than AccountPage::LAYOUTS, but comparing the object_id of each proved otherwise.

To correctly ensure that the two LAYOUTS are two different hashes, we could do the following:

 

Leave Your Observation

Your email address will not be published. Required fields are marked *