How to refer a local gem in ruby?
Asked Answered
P

2

8

I pack some ruby code into a gem. I want to refer the code in the gem in some other code. So in the Gemfile I specify the gem's name, version, and local path. Like:

gem 'gemname','0.x', :path => 'RELATIVE_PATH_TO_GEM_FILE'

After bundle install, I see

Using gemname (0.x) from source at RELATIVE_PATH_TO_GEM_FILE

But when I run the code, it can't find the code in the gem. LOAD_PATH shows ABSOLUTE_PATH_TO_GEM_FILE/lib.

No wonder it can't find the code, there's only gem file under ABSOLUTE_PATH_TO_GEM_FILE. it's not unpacked. So there's no lib directory.

if I gem install that gem file into my system, then all works fine. I can see the gem file was unpacked into source code files. But my question is if it can refer the local gem file directly somehow?

Photomechanical answered 22/6, 2011 at 18:37 Comment(3)
Normally, the term "local gem" means one you've compiled and installed locally, rather than downloading it from another website. Have you considered compiling and installing the gem locally, rather than doing what you're doing?Deformation
Actually, the .gem file was create by myself. I just packed some ruby code in that .gem file trying to make them easy to be shared with other code. And there is lib directory in the .gem file. I was thinking it will be like jar as to java. I suppose ruby can look into that package file rather than extract them all out of package. And confusingly, bundler actually find the source in the package. but load_path seems not support the directory in the package.Photomechanical
And by the way, though bundler has found that .gem file under my specific path, it won't cache the .gem in vendor/cache, unless the .gem file has been installed to system gemset.Photomechanical
E
9

No, you can't refer to a .gem file directly.

In your terminology, you need to use an "unpacked" gem.

:path => '/foo/bar/'

where /foo/bar/ is a (gem) directory with lib/, etc.

Embolden answered 22/6, 2011 at 19:11 Comment(0)
A
2

We made a local (not system-wide) gems location. We set these environment variables:

GEM_HOME=/path/to/rubygems-1.3.4
RUBYLIB=/path/to/rubygems-1.3.4/lib/

By setting those, we can then do 'gem install ...' to put the built gem into that directory, and ruby knows where to find them.

Aweinspiring answered 30/6, 2011 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.