Ale*_*ert 17
基本上有两种方式,
1)您可以使用以下命令加载gem中相对于Ruby文件的资源__FILE__:
def path_to_resources
File.join(File.dirname(File.expand_path(__FILE__)), '../path/to/resources')
end
Run Code Online (Sandbox Code Playgroud)
2)您可以添加从Gem到$LOAD_PATH变量的任意路径,然后步行$LOAD_PATH查找资源,例如,
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
Run Code Online (Sandbox Code Playgroud)
然后,
$LOAD_PATH.each { |dir| ... look for resources relative to dir ... }
Run Code Online (Sandbox Code Playgroud)