Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:40:04+00:00 2026-06-17T14:40:04+00:00

I have two files in two different directories: module MyModule def my_method path p

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:40:05+00:00Added an answer on June 17, 2026 at 2:40 pm

    __FILE__ always is the name of the file containing the __FILE__ variable, so saying my_method will always return where my_method is defined, not where MyClass calls it.

    You can probably get at the information you want using caller:

    module MyModule
      def my_method path
        p caller
      end
    end
    
    include MyModule # definition of MyModule
    class MyClass
      extend MyModule
      my_method 'my_file.yml'
    end
    
    my_class = MyClass.new
    

    Which outputs:

    ["test.rb:10:in `<class:MyClass>'", "test.rb:8:in `<main>'"]
    

    Edit:

    the caller array has only file names without paths…

    Well, I’d hoped you’d know how to work around that but….

    This is in test.rb:

    require './test2'
    class MyClass
      extend MyModule
      my_method __FILE__, 'my_file.yml'
    end
    
    my_class = MyClass.new
    

    This is in test2.rb:

    module MyModule
      def my_method path, file
        dir = File.dirname(path)
        p caller.map{ |c| File.join(dir, c) }
      end
    end
    

    Running test.rb outputs:

    ["./test.rb:4:in `<class:MyClass>'", "./test.rb:2:in `<main>'"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have files in two different directories that I would like to merge the
Basically, I have two files, in two different directories: index.php (in /login/) and index.php(in
I have two text files in two different languages and they are aligned line
I have two frames in different files (main frame and settings frame), and I'd
Hi I have two text files that each contain different information about certain structures.The
I have a small problem with Smarty... I have two different template files in
I have two different modules that need access to a single file (One will
I have two files client.php and server.php. The client file send a HTTP request
I have two files: domainList and config.cnf. The domainList file simply has a list
I have two directories with similar files, (one being a backup), what is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.