开发者

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

开发者 https://www.devze.com 2023-01-19 11:21 出处:网络
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 开发者_如何学Goit (array_extensions.rb), and in my config/applic

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 开发者_如何学Goit (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?


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.


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

0

精彩评论

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