In Ruby on Rails, after “gem install <gem_name>”, how to make it extend Array, or use its class method?

江枫思渺然 提交于 2019-12-13 04:13:08

问题


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



来源:https://stackoverflow.com/questions/5711869/in-ruby-on-rails-after-gem-install-gem-name-how-to-make-it-extend-array-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!