Ruby: Executing code only if file was called directly
I wanted a way to treat a Ruby file as both an importable library or a script. The best solution I could find was as follows:
if __FILE__ == $0 #script / run as self code goes here end If the file is called directly a la ruby ./library.rb, the code within that if block will be executed. If included, FILE, the file that this code exists in will not match $0, the file being executed and the code in that block will not be executed.
[Read More]