请给我红宝石脚本说明

时间:2012-02-14 11:24:47

标签: ruby-on-rails ruby ruby-on-rails-3

我对ruby一无所知,有人可以帮忙解释一下这个剧本的用途吗?

 #!/usr/bin/env ruby
 begin
    require '/usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler'
    # Check if an older version of bundler is installed(this part i understand)
    $:.each do |path|
    if path =~ %r'/bundler-0.(\d+)' && $1.to_i < 9
       err = "Please remove Bundler 0.8 versions."
       err << "This can be done by running `gem cleanup bundler`."
       abort(err)
    end
  end
  #it is from here that i dont understand
  require '/usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/lib/bundler/cli'
  Bundler::CLI.start
  rescue Bundler::BundlerError => e
    Bundler.ui.error e.message
    Bundler.ui.debug e.backtrace.join("\n")
  exit e.status_code
  rescue Interrupt => e
    Bundler.ui.error "\nQuitting..."
    Bundler.ui.debug e.backtrace.join("\n")
    exit 1
  end

这里我的irb测试:

/usr/lib/ruby/gems/1.9.1/gems/bundler-1.0.22/bin$ irb
irb(main):001:0> require 'bundler'
LoadError: no such file to load -- bundler
from (irb):1:in `require'
from (irb):1
from :0
irb(main):002:0> require 'bundler/cli'
LoadError: no such file to load -- bundler/cli
from (irb):2:in `require'
from (irb):2
from :0
irb(main):003:0> Bundler::CLI.start
NameError: uninitialized constant Bundler
from (irb):3
from :0
irb(main):004:0> 

感谢

2 个答案:

答案 0 :(得分:0)

它正在尝试启动捆绑器CLI,如果它获得BundlerError或被中断,它将打印堆栈跟踪并退出状态代码。从带有Gemfile的目录中的irb运行相同的代码,看看会发生什么。看起来如果从命令行执行bundle install会发生什么。


$ irb
ruby-1.9.3-p0 :001 > require 'bundler'
 => true 
ruby-1.9.3-p0 :002 > require 'bundler/cli'
 => true 
ruby-1.9.3-p0 :003 > Bundler::CLI.start

答案 1 :(得分:0)

我不确定你不明白...你能更具体一点吗?

但是rescue位是先前与begin相关的一个子句。这就像Java中的try / catch,如果这对您来说意味着什么。

这意味着,“尝试在beginend之间执行所有操作,但如果发生BundlerError,则执行此操作,如果发生Interrupt错误,那样做“

这会回答你的问题吗?