Ruby Language Special Constants in Ruby __FILE__

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Is the relative path to the file from the current execution directory
Assume we have this directory structure: /home/stackoverflow/script.rb
script.rb contains:

puts __FILE__

If you are inside /home/stackoverflow and execute the script like ruby script.rb then __FILE__ will output script.rb If you are inside /home then it will output stackoverflow/script.rb

Very useful to get the path of the script in versions prior to 2.0 where __dir__ doesn't exist.

Note __FILE__ is not equal to __dir__



Got any Ruby Language Question?