Core Extensions


I’ve seen a couple of people ask about why core extensions in Rails 3 are now extensions to the class. I haven’t had or heard of a clear enough answer so I tend to steer away. Until today.

Toby Tripp tweeted the following:

I think this is a fantastic point. How else am I able to determine if a method is Ruby core or Rails core extension easily otherwise, without referencing the documentation? I can imagine that the core extensions were done this way to add to the speed of loading them, but again, I do not know the clear reason.

Could someone clarify as to why it was done this way rather than extending the classes with modules that are included? Thank you!

ruby-1.9.2-head > "".method(:pluralize).source_location
=> ["/mnt/hgfs/ubuntu_shared/edge/vendor/rails/activesupport/lib/
active_support/core_ext/string/inflections.rb", 15]

But overall to me this decision certainly makes sense since the
which were created to store core extensions were kind of useless and
created deep nesting like ActiveSupport::CoreExtensions::String
obscuring the final purpose of exactly extending just String.

So IMO that's a scannability win.

We'll be moving these to a single extension module per class rather
than directly reopening classes.

This preserves the ability to just require
'active_support/core_ext/string/inflections' without having to
manually include the module, without leading to a proliferation of
extensions in the class' ancestor chains.

Plus, it makes documentation extremely clear and easy to read. All the
string extensions in one place.

And, this allows users to override core methods without needing to
alias_method_chain the Active Support override. The AS extension can
just call super to pick it up.