digitalWestie
digitalWestie

Reputation: 2797

Where do you extend classes in your rails application?

Just about to extend the Array class with the following extension:

class Array
  def shuffle!
    size.downto(1) { |n| push delete_at(rand(n)) }
    self
  end
end

However, I was wondering where a good place to keep these sort of extensions. I was thinking environment.rb or putting in its own file in the initializers directory.

Upvotes: 4

Views: 1144

Answers (1)

Greg Campbell
Greg Campbell

Reputation: 15302

I usually follow the ActiveSupport convention, which would be to place them in lib/core_ext/#{class}.rb - in this case, lib/core_ext/array.rb. As John Hyland notes, you can then require the file explicitly where needed, or put a require statement in initializers.

Upvotes: 11

Related Questions