The Rails method Array#to_sentence allows for the following:
['a', 'b', 'c'].to_sentence # gives: "a, b, and c"
I would like to extend this method to allow it to take a block, so that you can do something like the following (where people is an array of Person objects, which have the name attribute):
people.to_sentence { |person| person.name }
# => "Bill, John, and Mark"
I don’t have a problem with writing the extension method. But I can’t work out where to put it. The Rails core extensions get loaded somewhere down in the depths of ActiveSupport.
My need is for a place where user-defined code is always loaded, and is pre-loaded (before any application code).
Create
config/initializers/super_to_sentence.rb. All files in this directory are loaded after Rails has been loaded, so you’ll have a chance to override Rails’ definition ofArray#to_sentence.For code you want to load before Rails gets loaded, add it to
config/environment.rb.