问题描述
总结:在我 在您的 Gemfile 写入gem' naturalsort',:require => 'natural_sort_kernel'
这应该会有效果。
希望这有助于。
Summary: after I gem install <gem_name>, how do I make it extend Array which the gem can do?
Details:
I see a gem for "natural language sort", which is
http://rubygems.org/gems/naturalsort
http://naturalsort.rubyforge.org/
so I am using Ruby 1.9.2 and Rails 3.0.6, and I add the line
gem 'naturalsort'
into Gemfile, and do a bundle install and restart the server, but now, for some reason, I still can't do a
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
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. Is there a way to make it work, possibly by also modifying the Gemfile or something else?
In your Gemfile write
gem 'naturalsort', :require => 'natural_sort_kernel'
That should do the trick.
Hope this helps.
这篇关于在Ruby on Rails上,在“gem install< gem_name>”之后,如何使其扩展Array或使用其类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!