Yes Thomas, as the error messages indicate, he gets this in Alex’s web-based Console Editor extension. Specifically when Alex’s console passes the code text to eval()
.
I suggested the same test above 2 days ago.
EDIT: Just tested and everything works fine normally from the standard Ruby Console.
I put both files in a “alpha” subfolder of the “Plugins” folder, then loaded “beta.rb” …
load "alpha/beta.rb"
#=> 5
And the last entry afterward in $"
(aka $LOADED_FEATURES
) is …
"C:/Users/Dan/AppData/Roaming/SketchUp/SketchUp 2018/SketchUp/Plugins/alpha/alpha.rb"
… so Kernel#require_relative
works normally, without issues, and does not need the "./"
as Jim surmised above. Kernel#require_relative
first checks a path relative to the calling files path.
Prefixing a "./"
does not help as the "Plugins"
path is not the current working directory, nor is the calling file’s path the current working directory.
BUT, there is also a catch to using Kernel#require_relative
. That is that it MUST be used from within a file, not from a console. It apparently NEEDS __FILE__
or __dir__
to be valid and non-nil
.
For example typing the following into the standard Ruby Console gives …
require_relative "beta"
#=> Error: #<LoadError: cannot infer basepath>
#=> <main>:in `require_relative'
#=> <main>:in `<main>'
#=> SketchUp:1:in `eval'
The same errors for …
require_relative "alpha/beta"
The only thing that does work from the Console, is using Kernel#require
and a path relative to any in the $LOAD_PATH
array …
require "alpha/beta"
#=> 5
Changed Topic Title to: "Using require_relative from a Ruby console?"