0

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!

user1234
  • 21
  • 4

1 Answers1

0

Here's the problem, you're using require instead of require_relative to understand the difference see this. The easiest way to remember this is how I was taught require is for gems and libraries, require_relative is for files.

I also have some critique on your work if you don't mind:

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

This is very basic and you could go as far as having the information from the stack be given as an argument variable, you could also prompt the user for the information, etc. The concept of Ruby is OOP, the concept of OOP is making a system where everything works together successfully. That's my understanding of it anyways. I hope this answers your question and then some.


EDIT

You could also try installing the latest version of ruby, if you have rvm installed do rvm install ruby 2.3.0. This will install the latest version and allow you to run Ruby.

Community
  • 1
  • 1
13aal
  • 1,634
  • 1
  • 21
  • 47
  • 1
    will try this out, apparently using rvm 1.26.10 – user1234 Jun 29 '16 at 17:48
  • Ok, so after `rvm install ruby 2.3.0`, I got a new loaderror, looks like it changed from line 54 to line 55. Will show new error in edit 3 of OP. – user1234 Jun 29 '16 at 17:53
  • @user1234 Well the good news is we're getting farther down the lines.. lol – 13aal Jun 29 '16 at 17:54
  • 1
    *spongebob voice* 1 eternity later – user1234 Jun 29 '16 at 18:00
  • @user1234 Yeah we can be friends, go ahead and try to uninstall ruby completely. After it's uninstalled reinstall the latest version, and try again, you could also attempt `require 'rubygems'`; `require_relative '/full/path/to/the/file'` Go ahead and do the later first – 13aal Jun 29 '16 at 18:02
  • no success on the latter... how do you suggest I completely uninstall? And does the `which -a ruby` path matter? – user1234 Jun 29 '16 at 18:19
  • `which -a ruby` does matter, it should be `usr/local/bin/ruby` or `usr/bin/ruby` You could try to compile Ruby yourself and see what happens – 13aal Jun 29 '16 at 18:33