Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

ruby-2.0

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.

like image 751
monteirobrena Avatar asked Jun 04 '13 17:06

monteirobrena


2 Answers

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'
like image 111
monteirobrena Avatar answered Nov 13 '22 18:11

monteirobrena


I had the same issue. You could also use load 'lib/my_class.rb' require_relative assumes the .rb suffix and so, you don't have to write it out. load requires the whole full filename.

like image 45
aaronbnb Avatar answered Nov 13 '22 20:11

aaronbnb