I have two files in two different directories:
module MyModule
def my_method path
p File.join (File.dirname __FILE__), path
end
end
and
require_relative '../modules/mymodule' # definition of MyModule
class MyClass
extend MyModule
my_method 'my_file.yml'
end
I am getting output like my_home_dir/modules/my_file.yml but I want it to be my_home_dir/files/my_file.yml where files is the name of the directory where MyClass is defined.
I know I can use full path when I call my_method but is there a way for imported files to still have __FILE__ set to the name of the importing file?
Basically in my_method I need to have the full path of the file and I want to pass just a path relative to my calling file’s path.
__FILE__always is the name of the file containing the__FILE__variable, so sayingmy_methodwill always return wheremy_methodis defined, not whereMyClasscalls it.You can probably get at the information you want using
caller:Which outputs:
Edit:
Well, I’d hoped you’d know how to work around that but….
This is in test.rb:
This is in test2.rb:
Running test.rb outputs: