本文介绍了在Ruby on Rails上,在“gem install< gem_name>”之后,如何使其扩展Array或使用其类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:在我

在您的 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&lt; gem_name&gt;”之后,如何使其扩展Array或使用其类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:37