I'm trying to create my first gem and here's its structure:
riverbattle
├── lib
│ ├── riverbattle
│ │ ├── base.rb
│ │ ├── colorful.rb
│ │ ├── computer.rb
│ │ ├── constants.rb
│ │ ├── exit_error.rb
│ │ ├── field.rb
│ │ ├── game.rb
│ │ ├── human.rb
│ │ ├── invalid_move_error.rb
│ │ ├── move.rb
│ │ ├── player.rb
│ │ ├── version.rb
│ │ └── victory_error.rb
│ └── riverbattle.rb
└── riverbattle.gemspec
When I am in the directory rivarbattle/lib/riverbattle
and I run the command ruby base.rb
, everything starts working as it should be.
The file base.rb
is here:
require './game'
game = Game.new.start
But when I try to run it from the lib
directory using command ruby riverbattle.rb
, it fails with the following error:
/home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require': cannot load such file -- riverbattle/base (LoadError) from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from riverbattle.rb:1:in `'
The file lib/riverbattle.rb
look like this:
require 'riverbattle/base'
Despite the fact that the load fails I tried to build the gem with the following .gemspec file:
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "riverbattle/version"
Gem::Specification.new do |s|
s.name = 'riverbattle'
s.date = '2015-07-04'
s.summary = 'Enjoy the game!'
s.description = 'A small simple game called the Riverbattle'
s.platform = Gem::Platform::RUBY
s.version = '0.1.0'
s.authors = ["Denis Yakovenko"]
s.email = ["yakovenko.denis.a@gmail.com"]
s.require_paths = ["lib"]
s.license = "MIT"
end
After the successful build, I went to irb and tried require 'riverbattle'
and got the error:
LoadError: cannot load such file -- riverbattle
from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from (irb):1 from /home/denis/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `'
1) What am I doing wrong with require
that the files from different directories cannot be loaded? (After looking through stackoverflow questions I figured out that this is some problem with $LOAD_PATH, but I couldn't find out how to fix the code. BTW, such require
LoadErrors happen quite often for some reason and then I try to use require_relative
and sometimes it helps)
2) When I tried to lauch the gem in irb, the error was raised because of those initial problems with require
, right? Or something else is wrong?
3) If I want to add tests folder to the gem, can I just add the folder tests
to the root directory and copy all the rspec files there?
UPDATE
When I tried to run RUBYLIB="." ruby riverbattle.rb
in the terminal in the lib
folder, it gave me this:
RUBYLIB="." ruby riverbattle.rb /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require': cannot load such file -- ./field (LoadError) from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from /home/denis/WEB/Bursa/riverbattle/lib/riverbattle/game.rb:1:in<top (required)>' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/WEB/Bursa/riverbattle/lib/riverbattle/base.rb:1:in
' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /home/denis/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in
require' from riverbattle.rb:1:in `'
1.
#require
tries to load an absolute path, and if it fails it loads from$:
, to add your current directory run the script like this (bash, otherwise use apropriate method to define environment variables for your shell):In your
base.rb
require as follows:2. see #UPD section
3. why wouldn't you? (what's your specific problem with this?)
#UPD
The 2nd issue is because you miss
#files
in your gem specification, no files are included in your gem file, add something like this:#UPD2
Just to summarize here is working setup for your gem.