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

  • Home
  • SEARCH
  • 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 6954899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:40:01+00:00 2026-05-27T14:40:01+00:00

I have a Ruby script with path /foo/bar/gazook/script.rb . I also created a symlink

  • 0

I have a Ruby script with path /foo/bar/gazook/script.rb. I also created a symlink to it in $HOME/bin.

Now, I want my Ruby script to access some other file in directory /foo, and to keep paths relative, I have a variable FOO_DIRECTORY = File.expand_path(File.dirname(__FILE__) + "/../../") in my script.

The problem is that if I run my script from its symlink, this relative directory is wrong (since I guess its expanding from a different location).

How do I fix this? Is there a way besides using an absolute 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-05-27T14:40:02+00:00Added an answer on May 27, 2026 at 2:40 pm

    You can use File.readlink to resolve a symlink but you’ll want to check File.symlink? first.

    path = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
    

    Then you can work with path instead of __FILE__. You might want to use $0 instead of __FILE__ as well, __FILE__ is the current filename whereas $0 is the name of the current script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, the title say it all. I have a ruby script I want running
I'm trying to access a network path in my ruby script on a windows
I have a Ruby script which wants to determine its absolute path in order
so i have ruby script that simply writes to a test.txt file with hello
I have a Ruby script that generates a UTF8 CSV file remotely in a
I have a Ruby script in my Rails app that I use to load
I have a ruby script that does a few perforce operations (through the scripting
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have written a ruby script which opens up dlink admin page in firefox
I have a small ruby script in which I'd like to use ActiveRecord to

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.