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]

Ruby: Use .each on anything

While parsing several variables that could be strings or arrays of strings, I found myself doing too much type checking and my code was getting ugly. I wished there was a way to execute each on types that weren’t containers.

[Read More]