How to get a file contained in a gem "lib" directory?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I've build a gem "mylib" which contains a file called "myfile.xml" unde=
r=20
"lib/mylib" dir. Let's imagine that depending on the environment it is stor=
ed=20
(after installed) in:

/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.1/lib/mylib/myfile.xml

But of course there could be different versions of the gem:

/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.0/lib/mylib/myfile.xml
/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.1/lib/mylib/myfile.xml

Then I write a program which does:

require "mylib"

so the latest version (1.0.1) is loaded.

How would I get "myfile.xml" for the loaded "mylib" gem version? I'm lookin=
g=20
for a Gem class method like "Gem.bin_path(gemname)" but I cannot find it.

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
I

Iñaki Baz Castillo

El Lunes, 21 de Diciembre de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
Hi, I've build a gem "mylib" which contains a file called "myfile.xml"
under "lib/mylib" dir. Let's imagine that depending on the environment it
is stored (after installed) in:
=20
/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.1/lib/mylib/myfile.xml
=20
But of course there could be different versions of the gem:
=20
/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.0/lib/mylib/myfile.xml
/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.1/lib/mylib/myfile.xml
=20
Then I write a program which does:
=20
require "mylib"
=20
so the latest version (1.0.1) is loaded.
=20
How would I get "myfile.xml" for the loaded "mylib" gem version? I'm
looking for a Gem class method like "Gem.bin_path(gemname)" but I cannot
find it.

=46ound it:
Gem.required_location(gemname,"")

=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
A

Albert Schlef

Iñaki Baz Castillo said:
Hi, I've build a gem "mylib" which contains a file called "myfile.xml"
under
"lib/mylib" dir. Let's imagine that depending on the environment it is
stored
(after installed) in:

/usr/local/lib/ruby1.9/gems/1.9/gems/mylib-1.0.1/lib/mylib/myfile.xml
[...]
How would I get "myfile.xml" for the loaded "mylib" gem version?

You can put a method to return it in your mylib.rb:

def self.get_xml_pathname
File.join(File.dirname(__FILE__), 'mylib.xml')
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top