Best practice for adding a Ruby extension methods to Rails 3?

随声附和 提交于 2020-01-01 10:17:37

问题


I have an Array extension method I want to use in my Rails 3 project. Where should it live?

I have a app/classes where I originally put it (array_extensions.rb), and in my config/application.rb I load the path: config.autoload_paths += %W(#{Rails.root}/app/classes). However, when I drop to rails console the extension is not loaded.

Is there a pre-definded place I should put my extension methods for Rails 3? Or, a pre-definded way to add them? I know Rails has it's own extensions methods for Array. Should I add mine to active_support/core_ext/array/conversions.rb?

What's the best practice for Rails 3?


回答1:


The better way is create your extension in lib/core_ext directory to understand easyly where is your core_ext.

After create an initializer to require this file.




回答2:


All .rb files in config/initializers are required into the environment at startup; you should be putting extensions in there.



来源:https://stackoverflow.com/questions/3874148/best-practice-for-adding-a-ruby-extension-methods-to-rails-3

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