What is __FILE__ in Ruby?

09-Feb-2024 1022
While reading open source Ruby code, I often come across this mysterious looking __FILE__ variable (among others) that I never bothered to look into. After coming across it umpteenth time this evening, I decided to read up on it and here's everything I learned.__FILE__ is a magic variable that contains the filename with extension of the current file that's being executed. For example, if you're running code in a file named main.rb, __FILE__ will return "main.rb".
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle