rubyruby-2.6

Cannot load relative file in Ruby Script


Files are organized as follows:

- myScriptProject
  - solutions.ru
  - main.ru

solution.ru

def test_function
  puts 'Hello World'
end

main.rb

#!/Users/myusername/.rbenv/shims/ruby

require './solution' # or require_relative 'solution'

test_function

I was trying to run the scripts:

$ cd myScriptProject/
$ ruby main.ru

Then I got an LoadError message.

Traceback (most recent call last):
        2: from test.ru:3:in `<main>'
        1: from /Users/myusername/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/myusername/.rbenv/versions/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- ./solution (LoadError)

What could be the issue in this case?

By the way, my Ruby environment is managed by rbenv.

$ which ruby
$ /Users/myusername/.rbenv/shims/ruby

Solution

  • Ruby's script extension is rb not ru.