[英]In Ruby on Rails, after “gem install <gem_name>”, how to make it extend Array, or use its class method?
Summary: after I gem install <gem_name>
, how do I make it extend Array
which the gem can do? 简介:
gem install <gem_name>
,如何使它扩展gem可以做的Array
?
Details: 细节:
I see a gem for "natural language sort", which is 我看到了“自然语言排序”的瑰宝,
http://rubygems.org/gems/naturalsort http://rubygems.org/gems/naturalsort
http://naturalsort.rubyforge.org/ http://naturalsort.rubyforge.org/
so I am using Ruby 1.9.2 and Rails 3.0.6, and I add the line 所以我正在使用Ruby 1.9.2和Rails 3.0.6,然后添加以下代码行
gem 'naturalsort'
into Gemfile
, and do a bundle install
and restart the server, but now, for some reason, I still can't do a 进入
Gemfile
,并进行bundle install
并重新启动服务器,但是由于某种原因,现在我仍然无法执行
NaturalSort::naturalsort ['a', 'b', 'c', 'd', 'A', 'B', 'C', 'D']
in my helper file? 在我的帮助文件中? How can it be done?
如何做呢?
Also, it would be nice to just extend array so it can be done by arr.natural_sort
, but I have to add the line 另外,最好扩展数组,这样可以通过
arr.natural_sort
完成,但是我必须添加一行
require 'natural_sort_kernel'
to the beginning of the helper file. 到帮助文件的开头。 The good thing is that it works, but it is a bit messy to have
require
every where. 好的是它可以工作,但是在任何地方都
require
它有点混乱。 Is there a way to make it work, possibly by also modifying the Gemfile
or something else? 有没有办法通过修改
Gemfile
或其他方式使其工作?
In your Gemfile
write 在您的
Gemfile
写
gem 'naturalsort', :require => 'natural_sort_kernel'
That should do the trick. 这应该够了吧。
Hope this helps. 希望这可以帮助。
There is all so the following rail plugin 这样就可以了,下面的Rail插件
https://github.com/logandk/natural_sorting https://github.com/logandk/natural_sorting
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.