Active support core extensions (1-4)

Active Support is the Ruby on Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff.

It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Ruby on Rails itself.

By referring to this guide you will learn the extensions to the Ruby core classes and modules provided by Active Support.

1 How to Load Core Extensions
1.1 Stand-Alone Active Support
In order to have a near zero default footprint, Active Support does not load anything by default. It is broken in small pieces so that you may load just what you need, and also has some convenience entry points to load related extensions in one shot, even everything.

Thus, after a simple require like:

Require ‘active_support’
Objects do not even respond to blank?, let’s see how to load its definition.

1.1.1 Cherry-picking a Definition


most lightweight way to get blank? is to cherry-pick the file that defines it.

For every single method defined as a core extension this guide has a note that says where such a method is defined. In the case of blank? the note reads:

Defined in active_support/core_ext/object/blank. rb.

That means that this single call is enough:

Require ‘active_support/core_ext/object/blank’
Active Support has been carefully revised so that cherry-picking a file loads only strictly needed dependencies, if any.

1.1.2 Loading Grouped Core Extensions

The next level is to simply load all extensions to Object. As a rule of thumb, extensions to SomeClass are available in one shot by loading active_support/core_ext/some_class.

Thus, if that would do, to have blank? available we could just load all extensions to Object:

Require ‘active_support/core_ext/object’
1.1.3 Loading All Core Extensions

You may prefer just to load all core extensions, there is a file for that:

Require ‘active_support/core_ext’
1.1.4 Loading All Active Support

And finally, if you want to have all Active Support available just issue:

Require ‘active_support/all’
That does not even put the entire Active Support in memory upfront indeed, some stuff is configured via autoload, so it is only loaded if used.

1.2 Active Support Within a Ruby on Rails Application
A Ruby on Rails application loads all Active Support unless config. active_support. bare is true. In that case, the application will only load what the framework itself cherry-picks for its own needs, and can still cherry-pick itself at any granularity level, as explained in the previous section.

2 Extensions to All Objects
2.1 blank? and present?
The following values are considered to be blank in a Rails application:

Nil and false,
Strings composed only of whitespace, i. e. matching /As*z/,
Empty arrays and hashes, and
Any other object that responds to empty? and it is empty.
Note that numbers are not mentioned, in particular 0 and 0.0 are not blank.

For example, this method from ActionDispatch::Session::AbstractStore uses blank? for checking whether a session key is present:

The presence method returns its receiver if present?, and nil otherwise.

Active support core extensions (1-4)