Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the absolute path of the currently running Ruby script?

Tags:

ruby

I have a Ruby script which wants to determine its absolute path in order to find some data files that are stored relative to the script.

What's the simplest/best way to do this?

like image 786
Dean Povey Avatar asked Oct 18 '11 05:10

Dean Povey


People also ask

How do you find the absolute path of a file in Ruby?

Pass a string to File. expand_path to generate the path to that file or directory. Relative paths will reference your current working directory, and paths prepended with ~ will use the owner's home directory.

What is my current directory Ruby?

pwd : To check the current working directory, pwd(present working directory) method is used. 5. chdir : To change the current working directory, chdir method is used.

What is __ DIR __ in Ruby?

permalink #__dir__ ⇒ StringReturns the canonicalized absolute path of the directory of the file from which this method is called. It means symlinks in the path is resolved. If __FILE__ is nil , it returns nil . The return value equals to File.

How do I put the file path in Ruby?

Ruby has a method for this case. It is File::expand_path . Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point.


1 Answers

This can be done simply using:

File.expand_path $0
like image 157
Dean Povey Avatar answered Nov 15 '22 07:11

Dean Povey