« February 2008 | Main

March 03, 2008

ruby files as libraries or scripts

Listen to this article Listen to this article

Include the following snippet at the end of a Ruby file:

if $0 == __FILE__
    # do something
end

If you execute the file, the 'do something' block will run, but if you require the file, it won't. That way you can use the file as both a library and a script.