kernel_require.rb:54:来自Well Grounded Rubyist的'require'(LoadError)

时间:2016-06-29 14:29:47

标签: ruby rubygems require

我目前正在阅读David Black撰写的“Well Ground Ubuist”一书中的模块。当我继续练习时,我在尝试运行以下代码时偶然发现错误:

# stacklike.rb 
module Stacklike
  def stack
    @stack ||= []
  end
  def add_to_stack(obj)
    stack.push(obj)
  end
  def take_from_stack
    stack.pop
  end
end

# stack.rb
require "stacklike"
class Stack
  include Stacklike
end

s = Stack.new
s.add_to_stack("item one")
s.add_to_stack("item two")
s.add_to_stack("item three")

puts "Objects currently on the stack: "
puts s.stack

taken = s.take_from_stack
puts "Removed this object: "
puts taken

puts "Now on stack: "
puts s.stack

当我运行程序$ ruby stack.rb时,我收到以下错误:

/Users/myName/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in 'require': cannot load such file -- stacklike (LoadError) from /Users/myName/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in 'require' from stack.rb:2:in '<main>'

所以在花了一些时间在stackoverflow上并向这个用户提出类似的问题 - The #require method in modules mix-ins - 我尝试将require "stacklike"更改为require "./stacklike",但这不起作用,所以我试过这些:

require_relative "stacklike"
require "***I put the absolute path to the file here***"

但没有成功。然后,我看了看这里: no such file to load -- rubygems (LoadError),似乎我有两个红宝石版本...... 我跑了:$ which -a ruby,得到了以下输出:

/Users/myName/.rvm/rubies/ruby-2.2.0/bin/ruby
/usr/bin/ruby

我认为rubygems&amp;使用“require”时,两个ruby版本正在影响路径查找/加载位置。现在我想知道我是否必须删除一个版本或者我是否需要两个版本。我正在运行OS X Yosemite(10.10.2),当我运行ruby -v时,我得到:ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]

我需要两个版本吗?如果我需要删除一个ruby版本,我应删除哪一个以及如何删除?我尝试尽可能彻底,让我知道提供的其他信息。

编辑1:是的,两个文件(stack.rb,stacklike.rb)都在同一目录中

编辑2:我尝试使用require_relative“stacklike”,但它也不起作用。我也试过删除gem&amp;来自usr / bin的ruby,但也没有成功 - 我仍然得到相同的加载错误。

编辑3:运行rvm install ruby 2.3.0后,运行ruby stack.rb时收到以下输出加载错误:

`/Users/myName/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- stacklike (LoadError)
from /Users/myName/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from stack.rb:2:in `<main>'`

我将stack.rb第2行更改为:require_relative 'stacklike'

编辑4:
我跑了rvm implode然后brew install ruby。现在我得到了相同的错误但来自不同的路径/usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require':无法加载file - stacklike(LoadError)from /usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'from stack.rb:2 :在'

(现在我缺少rvm,但至少which -a ruby输出usr/local/bin/ruby

编辑5(最终编辑):我刚刚重新安装了ruby,创建了一个新目录&amp;文件,现在它以某种方式起作用...欢呼&amp;感谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

问题在于,您使用require代替require_relative来了解差异,请参阅this。记住这一点的最简单方法是我如何教授require用于宝石和图书馆,require_relative用于文件。

如果你不介意的话,我对你的工作也有一些批评:

require_relative "stacklike"

class Stack
  include Stacklike #<= Initiate the module
end

STACK_INFO = Stack.new #<= Turn the non-descriptive 's' into a more descriptive constant STACK_INFO

def add_info #<= Create a method to add more information to the stack
  STACK_INFO.add_to_stack("item one")
  STACK_INFO.add_to_stack("item two")
  STACK_INFO.add_to_stack("item three")
end  

def objects_in_stack? #<= Create a method for the information that's inside of the stack
  puts "Objects currently on the stack: "
  puts STACK_INFO.stack
end

def taken? #<= Create a method for what's taken from the stack
  taken = STACK_INFO.take_from_stack
  puts "Removed this object: \e[31m#{taken}\e[0m" #<= Makes colored console output 
end

add_info
puts objects_in_stack?
puts taken?
puts objects_in_stack?

module Stacklike
  #<= Spaces are important in Ruby, mostly because it makes Ruby readable.
  def stack
    @stack ||= []
  end

  def add_to_stack(obj)
    stack.push(obj)
  end

  def take_from_stack
    stack.pop
  end

end

这是非常基础的,你可以将堆栈中的信息作为参数变量给出,你也可以提示用户输入信息等.Ruby的概念是OOP,OOP的概念正在建立一个成功协同工作的系统。无论如何,这是我对它的理解。我希望这能回答你的问题,然后再回答一些问题。

修改

如果你安装了rvm,你也可以尝试安装最新版本的ruby rvm install ruby 2.3.0。这将安装最新版本并允许您运行Ruby。