Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 451
digitalWestie Avatar asked Dec 23 '22 02:12

digitalWestie


1 Answers

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.

like image 101
Greg Campbell Avatar answered Jan 06 '23 00:01

Greg Campbell