In Ruby on Rails, after “gem install ”, how t

2019-09-01 05:02发布

问题:

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?

回答1:

In your Gemfile write

gem 'naturalsort', :require => 'natural_sort_kernel'

That should do the trick.

Hope this helps.



回答2:

There is all so the following rail plugin

https://github.com/logandk/natural_sorting