Hey guys, I've been tooling around with Ruby the last days/weeks and I have learned some little lessons in general Ruby programming. These are some small tipps that I find very useful, especially for Ruby newcomers.

enhance the load path

These are 2 equally suited ways to put a folder in the load path. You may use it in your application to avoid using absolute paths.

[code lang="ruby"]
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
[/code]

use OptionParser for CLI apps

Option Parser is very useful if you need to evaluate CLI parameters in your script.

[code lang="ruby"]
opts = OptionParser.new
opts.on('-h', '--help') { RDoc::usage }
opts.on('-f', '--file FILEPATH') { |filepath| process(filepath) }
[/code]

RDoc

You should always use RDoc to comment your classes and methods but you can also use it to automatically create a help message for your CLI app. RDoc::usage will read and output it (see OptionParser example).

[code lang="ruby"]
# == Synopsis
#
# This is a sample app comment.
#
# == Usage
# app [ -h |--help ] [ -f | --file filepath ]
#
# filepath::
# a valid file path
#
# == Author
# John Doe
#
# == Copyright
# Copyright (c) 2011 John Doe
class Foo
...
end
[/code]