简体   繁体   English

如何在Rails 3中使用gem而不在Gemfile中引用它

[英]How to use a gem in Rails 3 without referencing it in the Gemfile

I'm wondering how to make a gem accessible in a Rails 3 app without putting a reference to it in the gemfile. 我想知道如何在Rails 3应用程序中访问gem而不在gemfile中引用它。 I want to do this with ruby-debug (I'm using ruby-debug19 ). 我想用ruby-debug(我正在使用ruby-debug19 )做到这一点。 I use this to debug, but not everybody on my team does and forcing the dependency just so I can use it doesn't seem very diplomatic. 我用它来调试,但不是团队中的每个人都这样做,并且强迫依赖只是为了使我可以使用它似乎不太外交。 Is there another way? 还有另一种方法吗?

If it ends up mattering, I'm using Rails3, Bundler 1.0.0, Ruby 1.9.2, RVM, OSX Snow Leopard 如果最后很重要,我正在使用Rails3,Bundler 1.0.0,Ruby 1.9.2,RVM,OSX Snow Leopard

On a side note, I had thought about using the gemfile group feature, but this doesn't feel right either. 附带一提,我曾考虑过使用gemfile组功能,但这也不对。 Groups seem great for things like factory_girl where there is an actual dependency albeit only in specfic environments, but with ruby-debug there is no real need for it to be there unless you want to use it. 组对于诸如factory_girl之类的东西似乎很棒,尽管只有在特定的环境中才有实际的依赖关系,但是对于ruby-debug来说,除非您要使用它,否则并不需要它。

This is really ugly and it does not pull it out of your gemfile, but I do this in my Gemfile so I can use 2 different sources for a gem in different environments: 这确实很丑陋,并且不会将其从您的gemfile中拉出来,但是我在我的Gemfile中执行了此操作,因此我可以在不同的环境中为gem使用2个不同的来源:

if ENV['MY_RDEBUG_VAR'] == "i_want_to_use_rdebug"
  gem "ruby-debug"
end

Just make it an obscure environment variable that only you would set on your system. 只需使其成为一个晦涩的环境变量,只有您可以在系统上进行设置。 That way, your coworkers will not have the env var set and the gem will never be installed or loaded for them. 这样,您的同事就不会设置env var,并且绝不会为他们安装或加载gem。

AFAIK you can't because Rails 3 isolates system Gems. 您无法做到这一点,因为Rails 3可以隔离系统Gems。

My suggestion is to add the Gem in the Gemfile with the :require => false option so it is included, installed but never loaded. 我的建议是使用:require => false选项将Gem添加到Gemfile中,以便包含,安装但从未加载它。 Then, in your app, require the gem manually. 然后,在您的应用中,手动需要gem。

Or you can try to bundle it in your project, within the vendor folder (there should be a folder for Gems) and ignore the specific gem folder in your SCM so that your co-workers won't be affected. 或者,您可以尝试将其捆绑在项目中的供应商文件夹(应该有Gems的文件夹)中,并忽略SCM中的特定gem文件夹,这样您的同事就不会受到影响。

I ended up adding the following code to a rails initializer and ignoring it in the .git/info/exclude file. 我最终将以下代码添加到Rails初始化程序中,而在.git / info / exclude文件中忽略了它。

$: << Bundler.bundle_path.join("gems", "ruby-debug19-0.11.6", "cli")
$: << Bundler.bundle_path.join("gems", "ruby-debug-base19-0.11.24", "lib")
$: << Bundler.bundle_path.join("gems", "linecache19-0.5.11", "lib")
$: << Bundler.bundle_path.join("gems", "columnize-0.3.1", "lib")
require 'ruby-debug'

While this works, I don't know if anybody should actually do this, accepting the fact that these dependencies need to be placed in the gem file is probably best. 虽然这样做有效,但我不知道是否有人应该这样做,因为最好将这些依赖项放置在gem文件中。

This is my own solution to using ruby_debug with both ruby 1.8 and 1.9 这是我将ruby_debug与r​​uby 1.8和1.9一起使用的解决方案

if RUBY_VERSION.include?('1.9')
  ruby_debug = 'ruby-debug19'  # new branch for ruby-debug that supports 1.9
else
  ruby_debug = 'ruby-debug'
  gem 'fastercsv' # ruby 1.9 bundles FasterCSV now
end


group :test do
  gem 'webrat'
  gem "factory_girl"
  gem 'shoulda'

  # gem 'redgreen'
  gem 'autotest-rails'
  gem 'autotest-fsevent'
  gem 'autotest'  
  gem ruby_debug
end

group :development do
  gem ruby_debug
end

I know this doesn't cover some other cases where it would be nice if there was a clean way to accomplish this -- autotest-growl and autotest-fsevent come to mind, as well as lots of things I require/rescue in my .irbrc for a nicer experience that I don't want to force on everyone, like wirble and hirb -- but specifically for ruby-debug , the ruby-debug-wrapper gem is helpful if testing against multiple Ruby versions is part of your pain. 我知道这并不涵盖其他一些情况,如果有一种干净的方法可以实现此目的,那就好了-我想到了autotest-growlautotest-fsevent ,以及我需要/救援的许多东西.irbrc可以提供更好的体验,我不想强​​加给所有人,例如wirble和hirb,但是特别是对于ruby-debug ,如果针对多个Ruby版本进行测试是您痛苦的一部分,那么ruby-debug-wrapper宝石将非常有用。

If you want to avoid installing any version of ruby-debug , I think options others have suggested are the only ways currently. 如果您想避免安装任何版本的ruby-debug ,我认为其他人建议的选择是当前的唯一方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM