This page explains how to add example/test scripts in Ruby packages, so that the Ruby package does not have to be in RUBYLIB


You have a ruby package and want to add an example script (for instance in an examples/ directory). So, your package layout is like:

  • lib/: the code
  • examples/: the example scripts

What you want is to be able to run the example scripts without having to add the lib/ directory to RUBYLIB (i.e. run the script after a fresh checkout for instance)


The following pattern can be used at the top of the script:

$LOAD_PATH.unshift File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))

This adds, inside the script, the lib/ directory in Ruby's search path, and makes sure that it gets a higher precedence than global files

Last modified 10 years ago Last modified on 08/15/11 17:49:02