My problem is the following:
I can't require a ruby file from any directory other than the one with all the main files, i.e. require
s like this: ./file
work fine, but require
s like this: dir_name/file
fail.
Here's my project structure:
├── bin
│ ├── console
│ └── setup
├── CODE_OF_CONDUCT.md
├── Gemfile
├── lib
│ ├── riverbattle
│ │ ├── base.rb <------- main file that starts everything
│ │ ├── 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 <------ the file from where I get the error
├── LICENSE
├── LICENSE.txt
├── Rakefile
├── README.md
├── spec
│ ├── spec_helper.rb
│ └── riverbattle_spec.rb
├── riverbattle-0.1.2.gem
└── riverbattle.gemspec
So, when I launch the app like this: ruby base.rb
when I am in the lib/riverbattle/
directory, everything launches fine, but when I try to do ruby riverbattle.rb
from the directory lib/
, I get the following error:
➜ lib git:(master) ✗ 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/.rvm/gems/ruby-2.2.1/gems/riverbattle-0.1.2/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/.rvm/gems/ruby-2.2.1/gems/riverbattle-0.1.2/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:2:in `'
I just don't get it why it cannot require one single file.
By the way, here's the content of the file (riverbattle.rb
) from which I get the error:
require "riverbattle/version"
module Riverbattle
require "riverbattle/base"
end
and nothing else.
I was told that require
actually requires an absolute path to the file and I need to do something with my $LOAD_PATH or something, but this information doesn't help at all to solve the issue. Why is it okay to require files from the same directory, but not okay, if I require them from another one?
1) Could anybody help me with solving that problem?
2) Is it the problem with only my computer and I can safely publish it as a gem (i.e. it may work properly on another computer), or it is the wrong code that doesn't work?