Today I Learned

hashrocket A Hashrocket project

Run Code if Current File is Calling File

This conditional tells Ruby to execute the code within if the current file is the file running the code.

# exec.rb
if __FILE__ == $0
  puts 'shown when exec.rb is run directly; not when required'
end

This works by comparing the current file (__FILE__) with the file that started the program ($0). If they aren't the same (exec.rb), the statement will not be printed.

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.