There are two ways:
1) You can load resources in relation to the Ruby file in your gem using __FILE__ :
def path_to_resources File.join(File.dirname(File.expand_path(__FILE__)), '../path/to/resources') end
2) You can add arbitrary paths from the Gem to the variable $LOAD_PATH , and then go $LOAD_PATH to find resources, for example,
Gem::Specification.new do |spec| spec.name = 'the-name-of-your-gem' spec.version ='0.0.1' # this is important - it specifies which files to include in the gem. spec.files = Dir.glob("lib/**/*") + %w{History.txt Manifest.txt} + Dir.glob("path/to/resources/**/*") # If you have resources in other directories than 'lib' spec.require_paths << 'path/to/resources' # optional, but useful to your users spec.summary = "A more longwinded description of your gem" spec.author = 'Your Name' spec.email = 'you@yourdomain.com' spec.homepage = 'http://www.yourpage.com' # you did document with RDoc, right? spec.has_rdoc = true # if you have any dependencies on other gems, list them thusly spec.add_dependency('hpricot') spec.add_dependency('log4r', '>= 1.0.5') end
and then
$LOAD_PATH.each { |dir| ... look for resources relative to dir ... }
Alex boisvert
source share