Marty Andrews

artful code

Monday, March 3, 2008

ruby files as libraries or scripts

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

if $0 == __FILE__# do somethingend

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.