Where do you extend classes in your rails application?

Just in order 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 is a good place to store such extensions. I thought environment.rb or put my own file in the initializer directory.

+4
source share
1 answer

I usually follow the ActiveSupport convention, which would have to place them in lib/core_ext/#{class}.rb - in this case lib/core_ext/array.rb . As John Hyland points out, you can either explicitly request a file where necessary, or place the require statement in initializers.

+11
source

Source: https://habr.com/ru/post/1311351/


All Articles