monteirobrena monteirobrena - 3 months ago 12
Ruby Question

How fix this error: kernel_require.rb:45:in `require': cannot load such file?

I have the following structure of files:


  • execute.rb

  • lib

    • my_class.rb




In the execute.rb I have the code bellow:

#!/usr/bin/ruby

require 'lib/my_class'

my_object= MyClass.new

my_object.some_method


And this is the code of my_class.rb:

class MyClass
def some_method
puts 'OK'
end
end


So, I tried run the execute.rb:

ruby execute.rb


But I receive this error:

/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- lib/my_class (LoadError)
from /home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from execute.rb:3:in `<main>'


Can anyone help me? I'll appreciate any help.
Thanks a lot.

Answer

I fix this following the hint of @Dogbert.

At execute.rb code it's necessary replace:

require 'lib/my_class'

for:

require_relative 'lib/my_class'