monteirobrena
monteirobrena

Reputation: 2620

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

I have the following structure of files:

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.

Upvotes: 6

Views: 18017

Answers (2)

aaronbnb
aaronbnb

Reputation: 69

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.

Upvotes: 1

monteirobrena
monteirobrena

Reputation: 2620

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'

Upvotes: 7

Related Questions