Rails come with a lot of initializers that we are not quite familiar with. One of them is the inflections initializer. In this post, we will look at what inflections.rb is and how we can use them.
What are Inflections?
Inflector is a part of the ActiveSupport library that provides methods to pluralize and singularize words in a given string. Inflections are rules that specify how words should be pluralized or singularized in a given context. These rules are defined in a file called “inflections.rb”.
This is what we would get in a new Rails 7 inflection file.
This file is commented out by default because the inflections are already defined in the Rails source code. Also, any new patch to these rules will not be accepted in the library to avoid breaking existing applications. If we need to provide extra rules, we can do so in the inflections.rb file.
ActiveSupport::Inflector
Before we understand the inflection rules, let’s take a look at the ActiveSupport::Inflector module.
This API is responsible for transforming words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.
Pluralization
To pluralize a word in the Rails application,
we can use the pluralize
method.
Singularization
Similarly, we can use the singularize
method to singularize a word.
There are a lot of other methods such as humanize
, titleize
, parameterize
that we will not be looking at in this post.
For more details, you can check out the Inflector API.
Inflections Rules
To understand the different inflection rules, we’ll be using some examples.
Irregular Inflections
Irregular inflections are words that do not follow the regular pluralization pattern.
Let’s assume we want to pluralize the word foot
to feet
and tooth
to teeth
.
Let’s try doing that in our Rails console:
As expected, the words are not pluralized correctly. We can fix this by adding the following rules in our inflections.rb file.
Now, let’s try again in the Rails console:
There are also plural and singular rules which are similar to the irregular rules.
Uncountable Inflections
Uncountable inflections are words that cannot be pluralized.
For this, we’ll take only one example,
let’s assume we have a ShoppingList
model which has item
and count
as attributes.
The item
attribute can be one of the following: apple
, banana
, milk
, bread
, and eggs
.
The count
attribute is the number of the given item
we want in the shopping list.
Now if we want to pluralize the item based on the count we would do the following:
Now if we try to pluralize the item, we would get the following:
Again, as expected, the pluralization is not correct for the word milk
.
Since milk
is uncountable, we will need to add a rule for it.
Now, if we were to pluralize milk
:
Acronym Inflections
Acronym inflections are words that are written in all caps.
We can specify inflector rules for these words as well.
Let’s take the example of HTML5 as an acronym.
Without specifying the rules,
if we were to use some of the inflector methods on HTML5
,
we would get some incorrect values:
To fix this, we need to specify HTML5
as an acronym.
Now, if we were to run the same methods:
Conclusion
In this post, we looked at the ActiveSupport::Inflector module and its different methods. We also looked at the different inflection rules and how to use them in our inflections.rb file.