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

I have the following file structure:

  • execute.rb
  • Lib
    • my_class.rb

In exec.rb, I have the following code:

#!/usr/bin/ruby require 'lib/my_class' my_object= MyClass.new my_object.some_method 

And this is the code my_class.rb:

 class MyClass def some_method puts 'OK' end end 

So, I tried running execute.rb:

 ruby execute.rb 

But I get 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 would appreciate any help. Thank you very much.

+6
source share
2 answers

I fix this by following @Dogbert's prompt.

In the execute.rb file, you need to replace:

 require 'lib/my_class' 

for

 require_relative 'lib/my_class' 
+7
source

I had the same problem. You can also use load 'lib / my_class.rb' require_relative assumes the suffix .rb, and therefore you do not need to write it out. Download requires full fully qualified file name.

+1
source

Source: https://habr.com/ru/post/946526/


All Articles