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.