Inspired by “Getting the source directory of a Bash script from within“, what’s the Ruby way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
For newer versions of Ruby, try:
__dir__For older versions of Ruby (< 2.0), the script being run can be found using:
File.dirname(__FILE__)– relative path; orFile.expand_path(File.dirname(__FILE__))– the absolute path.Note: Using
__dir__will return the script path even after a call toDir.chdir; whereas, using the older syntax may not return the path to the script.