Supercharging Ruby on Rails with Active Support Core Extensions

If you use Ruby on Rails, you’ve probably had that moment of surprise when you realize Rails already provides a method that does exactly what you needed. But have you ever wondered what’s behind those methods?

Today, we will talk about Active Support Core Extensions, an integral part of Ruby on Rails. Its purpose is to provide extensions and utilities that give Ruby even more superpowers. Active Support extends Ruby’s native classes and modules with additional methods, making development in Rails more intuitive and powerful.

For example, you’ve probably used the .blank? method, that isn’t part of Ruby’s core; it’s provided by Active Support in Ruby on Rails. Active Support adds the blank? method to many Ruby objects, allowing you to easily check if an object is “blank,” which means it’s either nil, empty, or a whitespace string, depending on the object type.

nil.blank? # true
['A'].blank? # false
false.blank? # true

Without Active Support, these methods wouldn’t be available in standard Ruby.

Loading the extensions

To keep the default footprint as small as possible, Active Support loads the minimum dependencies by default. So, with a simple require like the one below, only the extensions needed by the Active Support framework itself are loaded.

require "active_support"

Active Support is broken into small pieces, allowing you to load only the specific extensions you need. It also offers convenient entry points for loading related extensions in one go — or even everything at once.

For example, if you only need the extension to convert strings to Time objects (super useful when working with APIs, for instance):

require 'active_support/core_ext/string/conversions'

"2024-08-19".to_time # => 2024-08-19 0000 -0300

Or if you want to load all the extensions at once:

require 'active_support/all'

Introducing Active Support Extensions

Active Support comes packed with a ton of useful extensions. Here are just a few that you probably use already without even realizing where they come from.

Time Calculations

If you work with times and dates (like in an app that handles events or reminders), you’re probably using things like:

5.days.ago
2.hours.from_now

String Inflections

Converting strings to different formats is something we do all the time, and Active Support makes it super easy:

"active_support".camelize # => "ActiveSupport"
"Post".underscore # => "post"

Hash Slicing

When dealing with large hashes, selecting only the keys that matter is a breeze:

my_hash = { a: 100, b: 200, c: 300 }
my_hash.slice(:a, :c) # => { a: 100, c: 300 }

Duplicating objects

As of Ruby 2.5, most objects can be duplicated via dup or clone:

"something".dup     # => "something"
"".dup # => ""
10.dup # => 1
10.method(:+).dup # => TypeError (allocator undefined for Method)

Active Support provides duplicable? to query an object about this:

"something".duplicable?     # => true
"".duplicable? # => true
10.method(:+).duplicable? # => false

I encourage you to check out the official Active Support Core Extensions guide where you’ll find many more examples.