Absolute path for file in a gem

Ben Koski's Avatar

Ben Koski

23 Nov, 2010 05:40 PM

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

  1. Support Staff 2 Posted by John Barnette on 23 Nov, 2010 05:48 PM

    John Barnette's Avatar

    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?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac