Absolute path for file in a gem
I'm building a gem that includes an ERB template. To use this inside Rails, I need the absolute path to this file.
I don't want to use Gem.required_location because I want the gem version requirements to live in the initial require.
So far, I've come up with
File.join(Gem.loaded_specs['my_gem'].full_gem_path, 'lib',
'templates', 'mytemplate.erb')
but is there a better way? It seems like Gem::Specification should have some method to determine the absolute path to a file contained within.
Thanks!
Ben
Support Staff 2 Posted by John Barnette on 23 Nov, 2010 05:48 PM
Why are you forcing your code to be in a Gem at all? Since you're being a good Ruby citizen and putting your templates under
lib, why not figure out the paths relative to__FILE__in a helper in the same directory structure?