I am currently reading about modules in the "Well Grounded Rubyist" book by David Black. As I follow along to an exercise, I stumble upon an error when trying to run the following code:
# 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
And when I run the program, $ ruby stack.rb
, I get the following error:
/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>'
So after spending some time on stackoverflow and having a similar problem to this user - The #require method in modules mix-ins - I tried changing: require "stacklike"
to require "./stacklike"
, but that didn't work, so I tried these:
require_relative "stacklike"
require "***I put the absolute path to the file here***"
but no success. Then, I looked here:
no such file to load -- rubygems (LoadError), And it seems that I have two ruby versions...
I ran: $ which -a ruby
, and got the following output:
/Users/myName/.rvm/rubies/ruby-2.2.0/bin/ruby
/usr/bin/ruby
I think rubygems & the two ruby versions are affecting the path lookup/load locations when using "require". Now I am wondering whether I must delete one version or if I need both. I am running OS X Yosemite (10.10.2), and when I run ruby -v
, I get: ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
Do I need both versions? If I need to delete one ruby version, which one should I delete and how? I tried being as thorough as possible, let me know what other information to supply.
Edit 1: Yes, both files (stack.rb, stacklike.rb) are in the same directory
Edit 2: I tried using require_relative "stacklike", but it doesn't work either. I also tried removing gem & ruby from usr/bin, but no success there either - I still get the same load error.
Edit 3: After running rvm install ruby 2.3.0
, I received the following output load error when running 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>'`
I changed stack.rb line 2 to: require_relative 'stacklike'
Edit 4:
I ran rvm implode
then brew install ruby
. Now I get the same error but from a different path /usr/local/Cellar/ruby/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such 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:in '
(now I'm missing rvm
but at least which -a ruby
outputs usr/local/bin/ruby
)
Edit 5 (FINAL EDIT): I just reinstalled ruby, created a new directory & files, and now it somehow works... cheers & thanks to all for your help!