使用NoMethodError自定义Gem执行失败

我写了我的第一个gem(ruby noob alert!)。 要将它部署到我的本地gem目录中,我运行:

bundle install bundle exec rake install 

此时我尝试从命令行运行我的gem。 我得到一个神秘的错误:

 my_gem Gem File Location: C:/ruby193/lib/ruby/gems/1.9.1/gems/my_gem-0.0.1/Gemfile fatal: Not a git repository (or any of the parent directories): .git getting Gem.bin_path() C:/ruby193/lib/ruby/gems/1.9.1/gems/bundler-1.6.3/lib/bundler/rubygems_integration.rb:305:in `block in replace_bin_path': undefined method `name' for nil:NilClass (NoMethodError) from C:/ruby193/lib/ruby/gems/1.9.1/gems/my_gem-0.0.1/bin/my_gem:20:in `' from C:/ruby193/bin/my_gem:23:in `load' from C:/ruby193/bin/my_gem:23:in `' 

当我运行bundle install时,它用它自己的代码替换了bin / my_gem的内容。 我确实添加了修改此文件稍微尝试调试。 失败似乎发生在Gem.bin_path中。 以下是bin / my_gem的内容:

 #!/usr/bin/env ruby # # This file was generated by Bundler. # # The application 'my_gem' is installed as part of a gem, and # this file is here to facilitate running it. # require 'pathname' ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath) puts "Gem File Location: " puts(ENV['BUNDLE_GEMFILE']) require 'rubygems' require 'bundler/setup' puts "getting Gem.bin_path()" path = Gem.bin_path('my_gem', 'my_gem') # this is the line that causes the error puts "Gem.bin_path(): #{path}" load Gem.bin_path('my_gem', 'my_gem') 

除了重新开始使用新gem之外,我不知道接下来要做什么。 有任何想法吗?

看起来你遇到了这个问题https://github.com/bundler/bundler/issues/2838

因为Bundler会记住传递给install标志,如果你曾经运行过bundle install --binstubs ,它会在后续的bundle install运行中重新生成binstubs。 您可以运行bundle config --delete bin来删除该设置,然后恢复原始可执行文件。