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?
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 req开发者_JS百科uire
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.
There is all so the following rail plugin
https://github.com/logandk/natural_sorting
精彩评论