开发者

Where to put the extension methods(loaded only once) in rails application

开发者 https://www.devze.com 2023-03-12 11:55 出处:网络
I开发者_高级运维 have couple of helper methods added to the existing classes, and i want them to be loaded only once. for example i have a except method

I开发者_高级运维 have couple of helper methods added to the existing classes, and i want them to be loaded only once. for example i have a except method

class Array
  def except(array)
    self.select do |item|
      array.exclude? item
    end
  end
end

and would like to call it from different views & controllers like this

 a= [1,2,3,4]
 b=a.except [1,3]


Put it into a file in config/initializers. These are loaded in alphabetic order; so if some other code uses it during initialisation, just use a file like config/initializers/000_important_monkey_patches.rb

0

精彩评论

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