简体   繁体   English

在Ruby on Rails中,安装“ gem”后 <gem_name> ”,如何使其扩展Array,或使用其类方法?

[英]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? 简介: gem install <gem_name> ,如何使它扩展gem可以做的Array


Details: 细节:

I see a gem for "natural language sort", which is 我看到了“自然语言排序”的瑰宝,

http://rubygems.org/gems/naturalsort http://rubygems.org/gems/naturalsort
http://naturalsort.rubyforge.org/ http://naturalsort.rubyforge.org/

so I am using Ruby 1.9.2 and Rails 3.0.6, and I add the line 所以我正在使用Ruby 1.9.2和Rails 3.0.6,然后添加以下代码行

gem 'naturalsort'

into Gemfile , and do a bundle install and restart the server, but now, for some reason, I still can't do a 进入Gemfile ,并进行bundle install并重新启动服务器,但是由于某种原因,现在我仍然无法执行

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 另外,最好扩展数组,这样可以通过arr.natural_sort完成,但是我必须添加一行

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. 好的是它可以工作,但是在任何地方都require它有点混乱。 Is there a way to make it work, possibly by also modifying the Gemfile or something else? 有没有办法通过修改Gemfile或其他方式使其工作?

In your Gemfile write 在您的Gemfile

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

That should do the trick. 这应该够了吧。

Hope this helps. 希望这可以帮助。

There is all so the following rail plugin 这样就可以了,下面的Rail插件

https://github.com/logandk/natural_sorting https://github.com/logandk/natural_sorting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁决定什么进入“宝石安装 <gem_name> “你如何决定使用哪个叉子? - Who decides what goes into “gem install <gem_name>” and how do you decide which fork to use? 如果Rails应用程序使用“ require&#39;gem_name&#39;”而不是“ gem&#39;gem_name&#39;”或“ config.gem&#39;gem_name&#39;”,会更快吗? - If a Rails app uses “require 'gem_name'” instead of “gem 'gem_name'” or “config.gem 'gem_name'”, will it be faster? 很多:`require&#39;:无法加载这样的文件 - 升级ruby / rails后的“gem_name”(LoadError) - Many: `require': cannot load such file — “gem_name” (LoadError) after upgrade of ruby/rails Rails-扩展红宝石宝石 - Rails - Extend a Ruby Gem Ruby on Rails 4,宝石安装 - Ruby on Rails 4, Gem Install 对于Ruby on Rails,而不是添加“require” <gem_name> &#39;“每个地方,将它们全部添加到config / environment.rb是否合适? - For Ruby on Rails, instead of adding “require '<gem_name>'” every where, is it good to add them all to config/environment.rb? Ruby on Rails-如何在Windows中安装水豚宝石? - Ruby on Rails - how to install capybara gem in Windows? 用于 ruby​​/rails 的 OCR tesseract gem - 如何安装? - OCR tesseract gem for ruby/rails - How to install? Ruby和Rails gem安装问题 - Ruby and Rails gem install issues 在ruby gem中安装rails资产 - Install rails assets in a ruby gem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM