Bundler如何在Ruby中隔离宝石?

时间:2014-05-11 11:13:12

标签: ruby bundler

' bundler',流行的ruby gem操作工具,将gem安装到与普通gem命令相同的目录中。但是使用bundle exec执行的应用程序只能访问使用bundler安装的gem,而不能访问"系统范围内的#34;宝石。这是如何在技术上完成的? Bundler monkey-patch require,或者某些路径操作,还是什么?

2 个答案:

答案 0 :(得分:1)

Bundler确保Ruby可以找到Gemfile中的所有gem(及其所有依赖项)。它通过配置加载路径来实现,因此可能需要Gemfile中的所有依赖项。这是通过以下调用完成的:

Bundler.setup

ruby​​ LOAD_PATH是文件系统中ruby将查找文件的位置列表,如果你需要的话。我认为它类似于Linux / Windows操作系统中的PATH变量,但是对于ruby。

要查看此操作,请以root用户身份运行load_path。我是从我的mac

这样做的
$ sudo ruby -e 'p $LOAD_PATH'
["/Library/Ruby/Site/2.0.0", "/Library/Ruby/Site/2.0.0/x86_64-darwin13", "/Library/Ruby/Site/2.0.0/universal-darwin13", "/Library/Ruby/Site", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0/x86_64-darwin13", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby/2.0.0/universal-darwin13", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/vendor_ruby", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/x86_64-darwin13", "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin13"]

现在,从rails app中运行相同的命令:

$ ruby -e 'p $LOAD_PATH' #paths on new rails 4.1 app
["/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/uglifier-2.5.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/turbolinks-2.2.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/extensions/x86_64-darwin-12/2.1.0-static/sqlite3-1.3.9", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sqlite3-1.3.9/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sdoc-0.4.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sass-rails-4.0.3/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sass-3.2.19/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/rdoc-4.1.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/rails-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sprockets-rails-2.0.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/sprockets-2.11.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/tilt-1.4.1/lib", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/gems/2.1.0/gems/json-1.8.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/jquery-rails-3.1.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/jbuilder-1.5.3/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/hike-1.2.3/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/coffee-rails-4.0.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/railties-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/thor-0.19.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/coffee-script-2.2.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/execjs-2.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/coffee-script-source-1.7.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/arel-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/activerecord-deprecated_finders-1.0.3/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/activemodel-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/actionmailer-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/mail-2.5.4/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/treetop-1.4.15/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/polyglot-0.3.4/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/mime-types-1.25.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/rack-test-0.6.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/rack-1.5.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/erubis-2.7.0/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/builder-3.1.4/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/activesupport-4.0.2/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/tzinfo-0.3.39/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/thread_safe-0.3.3/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/multi_json-1.10.0/lib", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/gems/2.1.0/gems/minitest-4.7.5/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/i18n-0.6.9/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1/gems/rake-10.3.1/lib", "/Users/gaurish/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.6.1/lib", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/x86_64-darwin12.0", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby/2.1.0", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby/2.1.0/x86_64-darwin12.0", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0", "/Users/gaurish/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/x86_64-darwin12.0"]

注意,加载路径如何被修改为完全指向Gemfile

中指定的gem版本

您可能会注意到bundler会调整此加载路径,因此它指向Gemfile中给出的依赖项。因此,无论何时运行require,ruby将仅在LOAD PATH中给出的提取版本,并记住在Bundler.setup调用期间由捆绑器设置/修改此加载路径。因此,根据Gemfile,它可以准确地将ruby指向它所希望的任何文件版本,正如您从上面的示例中看到的那样,当我们以rails用户身份打印rails应用程序的rails路径和rails应用程序之外

想知道这回答了你的问题吗?

答案 1 :(得分:-1)

我根据以下链接在Rubygems上构建并发布了我的第一个gem: