Ruby-on-rails – adding a method to built-in class in rails app

rubyruby-on-rails

I want to add a method to the Array class in a rails app. Where should I put this method?

EDIT to be clearer, obviously I put it in a file somewhere, but how do I tell the rails app about where to find it?

Best Solution

One way to do this is to create a file at lib/rails_extensions.rb. Then, add your extensions like so:

class Array
  def bring_me_food
    # ...
  end

  def make_tea
    # ...
  end
end

class Hash
  def rub_my_shoulders
    # ...
  end
end

Then in config/environment.rb, add this:

require 'rails_extensions'

Your mileage with subservient objects may vary.