下面是我创建的一个 gem,目的是打开一个 text/README.md 文件并拉出第一行和最后一行。gem 构建正确,但是如果我尝试运行一个单独的文件来调用我构建的 gem,我会收到以下错误消息:
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/nrodruck_test_gem-0.0.1/bin/test_gem:8:in
`<top (required)>': uninitialized constant TestGem::Myfile (NameError)
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/test_gem:19:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/test_gem:19:in `<main>'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `eval'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'
下面是我试图用来运行我的 gem 的文件。该文件需要一个命令行,您可以在其中指定您希望我的 gem 读取的文件。
#!/usr/bin/env ruby
require 'test_gem'
puts "test_gem script executing"
puts ARGV[0]
puts TestGem::MyFile.first( ARGV[0] )
这是宝石:
$LOAD_PATH.unshift File.expand_path("../test_gem", __FILE__)
require 'version'
require 'hello'
module TestGem
puts "module TestGem defined"
class MyFile
def self.first(filename)
File.open(filename, "r") {|f| f.readline}
end
def self.last(filename)
File.open(filename, "r")[-1]
end
end
end
编辑
根据评论修复了错字,仍然收到相同的错误消息。
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/nrodruck_test_gem-0.0.1/bin/test_gem:8:
in `<top (required)>': uninitialized constant TestGem::Myfile (NameError)
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/test_gem:19:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/test_gem:19:in `<main>'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `eval'
from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'