开发者

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 pu开发者_开发百科tting in its own file in the initializers directory.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜