Where is the right place to put predefined class (e.g. String, Symbol) extensions in Ruby on Rails?

青春壹個敷衍的年華 提交于 2019-12-24 23:31:08

问题


I would like to add my_method to the Symbol class, and be able to call my_method from app/helpers/application_helper.rb:

module ApplicationHelper
  def my_helper
    my_symbol.my_method
  end
end

Where is the most appropriate place to put:

class Symbol
  def my_method
    <some code here>
  end
end

?


回答1:


I think that sticking your native class extensions in a new file in your lib folder and require-ing them in your environment.rb file should do it.




回答2:


I typically create a file named monkey_patches.rb (or similar) - so it's very obvious where the patches are - then load it with an initializer in config/initializers. That's what they're for!



来源:https://stackoverflow.com/questions/4425620/where-is-the-right-place-to-put-predefined-class-e-g-string-symbol-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!