简体   繁体   English

向Rails 3添加Ruby扩展方法的最佳实践?

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

I have an Array extension method I want to use in my Rails 3 project. 我有一个我希望在我的Rails 3项目中使用的数组扩展方法。 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) . 我有一个app / classes,我最初放在它(array_extensions.rb),在我的config / application.rb中我加载路径: config.autoload_paths += %W(#{Rails.root}/app/classes) However, when I drop to rails console the extension is not loaded. 但是,当我放入rails console ,未加载扩展。

Is there a pre-definded place I should put my extension methods for Rails 3? 是否有一个预定义的地方我应该为Rails 3添加我的扩展方法? Or, a pre-definded way to add them? 或者,一种预先定义的添加方式? I know Rails has it's own extensions methods for Array. 我知道Rails有它自己的Array扩展方法。 Should I add mine to active_support/core_ext/array/conversions.rb ? 我应该将我的内容添加到active_support/core_ext/array/conversions.rb吗?

What's the best practice for Rails 3? Rails 3的最佳实践是什么?

The better way is create your extension in lib/core_ext directory to understand easyly where is your core_ext. 更好的方法是在lib/core_ext目录中创建扩展, lib/core_ext了解core_ext的位置。

After create an initializer to require this file. 创建初始化程序后需要此文件。

All .rb files in config/initializers are required into the environment at startup; config/initializers中的所有.rb文件在config/initializers都需要进入环境; you should be putting extensions in there. 你应该在那里放置扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM