How to refer a local gem in ruby? - ruby

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?

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.

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.

Related

How to install gem to local path and then require it

I want to setup gem from rubygems.org to some subfolder of my project like "/gems" and then use it from script via require. Please help me resolve it.
The preferred way is to go with a standard scenario: you build a gem, with binaries, pack it and when used it will download all the dependencies itself, according to gemspec. If the above is for some reason non-acceptable for you, one of the options would be:
• You create a gem for your script;
• You specify a path where to install dependencies:
bundle install --path=vendor/gems
• Instead of require you use require_relative in your script because on destination machines there won’t be local bundle’s config file, pointing to the right folder;
• As your script is finished, you pack everything including gems.
The normal way to go in a ruby project is to setup a Gemfile and use bundler (see link for more info) to handle the gems your project requires, without having to think of where they are stored
However, if there really is no way around shipping an own gem-directory, e.g. because your productive system has absolutely no way to access the internet, you could make it this way:
Create a directory in the root of your project (e.g. 'gems').
Download and unpack the gems to that directory (or use the appropriate options of gem install to redirect the installation into that directory)
Create a ruby file in the root directory of your project with following constants:
PROJECT_DIR = __dir__
GEMS_DIR = File.join(PROJECT_DIR, 'gems')
Now you can require your gems link require File.join(GEMS_DIR, <gem_name>)
Nonetheless, you should really think about using bundler, if possible in any way.
EDIT: to install gem via gem install
Delete the PG data from your project's gems directory
uninstall global gem gem uninstall pg
Install pg again, but into your project's directory: gem install -i <path_to_projects_gems_dir> pg
Run you script again with the require pointing to the gem stated above
I solved it like this:
gem install -i ./ pg
ROOT = File.expand_path('..', __FILE__)
ENV['GEM_PATH'] = File.join(ROOT, './')
require 'pg'
puts 'Version of libpg: ' + PG.library_version.to_s

How to pick gems from a specific directory

I forked a gem I use a lot in order to code some enhancements on it. I want it to be installed in a subfolder called ~/codebase/ruby, where I keep all my Ruby projects, arranged in subdirectories.
To do that, I built and installed the gem with the following commands:
gem build my_gem.gemspec
gem install mygem-x.x.gem -i./mygem
mygem is installed in ~/codebase/ruby/mygem. I can't get my client code (which is in another directory) to grab mygem from there.
I've tried all of the following without success:
Running ruby with argument -I<path_to_mygem>.
Adding <path_to_mygem> to PATH.
Putting :gempath: <path_to_mygem> into ~/.gemrc.
I know I could put the gem in ~/.gem/ruby/<version> (since it appears in gem env) and that would probably work, but that would break my existing directory structure of Ruby code, forcing me to code in a different directory only for mygem, which is something I want to avoid unless it's the only option.
Thoughts?
Hopefully you are using Bundler to load the required gems. Then you just specify the path to the gem file in your Gemfile:
gem 'my_gem', :path => '~/codebase/ruby/mygem'

Local gem repository? Install a ruby gem without 'gem'?

Is it possibile to have something like a 'local' gem repository for Ruby?
I'm working on a custom Linux distribution without admin rights. Ruby is installed on the machine (v.1.8.7) but apparently no 'gem' or 'bundle' or whatever are installed. I need to use some Ruby gems like Nokogiri.
Is it possible to use Ruby gems without installing them through gem install?
Yes. Any gem can be used standalone. You just have to either download the source from github, or download the gem and extract its contents manually.
After you've done that you have to add the lib folder of the gem into the load path ($:) of Ruby. For example:
$:.unshift(File.expand_path("nokogiri-1.6.1/lib"))
require 'nokogiri'
Assuming you are running Ruby in the current directory and the Nokogiri source is in the folder nokogiri-1.6.1.
But remember that first you have to do the same with all Nokogiri prerequisites. I.e. all the libraries Nokogiri depends on.
Another option, which is what I would do, is to install RubyGems in your home directory. That can get a little bit tricky though, but it's possible by downloading the RubyGems package and running something like:
ruby setup.rb --prefix=/home/my_user_name/rubygems
Then you need to set up the ENV variables GEM_HOME and GEM_PATH to point to a location in your home directory where you want all your gems to be installed. See "What's the difference between GEM_HOME and GEM_PATH?" for a description of what they do.
You will also need to add something like /home/my_user_name/rubygems/bin to your PATH so that the gem command can be found by the shell.

Viewing a Gem's Source Code

Ruby dabbler/newbie here who's not familiar with the ecosystem, so apologies if this is one of those super duh questions.
Is there a way to view all the files and/or source code installed by a gem? That is, I just ran
$ gem install sass
And the sass gem is now a part of my local system
$ gem list --local
...
sass (3.1.16, 3.1.2)
...
I want to know what the gem install command put on my system. Is there a command I can run to see all the files installed by the gem?
After some googling, man gem and gem help commands, I discovered the contents command.
$ gem contents sass
However, when I run this command with the aforementioned sass gem, I get the following results
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/engine_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/functions_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/extend_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/logger_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/css2sass_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/subset_map_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util/multibyte_string_scanner_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/callbacks_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/importer_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/css_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/scss_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/scss/rx_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/util_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/script_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/less_conversion_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/cache_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/test/sass/plugin_test.rb
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/sass-convert
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.16/bin/scss
However, this list seems incomplete as I know there are files in
.../.rvm/gems/ruby-1.9.2-p180/gems/sass-3.1.2/lib/
Why does contents not show the files from lib?
Is it possible for a gem installer to install files outside of the gems folder?
Is there a command that can show everything installed by a gem?
gem has an unpack command: http://guides.rubygems.org/command-reference/#gem-unpack
gem unpack rake
ls rake-0.4.3/
There are two really good ways to do this. There is another gem which allows you to open the gem and edit. This gem is call gem-open
gem install gem-open
then
gem open sass
Another way is to generate your own rdocs.
gem rdoc sass
You can then look at your rdocs by
gem server
Also if you are using rvm, you can type rvm info and it will show GEM_HOME location.
This will be where all your gems source code is.
cd $GEM_HOME
cd gems/sass-3.1.2/
Update:
This is the way I mostly do this now, when using bundler.
cd $(bundle show sass)
This will be the version of sass in your Gemfile.
I usually open a gem by running this command from the console
EDITOR=<your editor> bundle open <name of gem>
The lib/ directory you mentioned is for version 3.1.2 of the gem; gem contents without --version will just list one version (it appears to pick the newest version, but I'm unable to verify this is always true). What output do you get for gem contents --version 3.1.2 sass?
You can also use just rename the .gem file to .tar and extract as a posix archive. The source code is inside it in the lib folder. See https://blog.srcclr.com/extracting-ruby-source-code-from-gem-packages/ for more details.
In addition to gem contents, another command you might find useful is gem environment. If you have multiple paths for your gem installations, they will all be listed under the "GEM PATHS" label.

Bundler: installing a specific .gem file

Is there a way to tell Bundler to install a particular .gem file I have lying around?
I have a compiled version of ParseTree that I must use (damn you, Windows!), but didn't want to expand the gem file in order to add a :path => '...' attribute to the 'gem' requirement.
You could also package your gems with bundle package, which puts all of your gems in the vendor/cache directory of your project. If needed, overwrite ParseTree with your precompiled gem in in that directory. Then, when you set up your project on another machine, run bundle install --local and it will only install gems that you've packaged.
Instead of setting up your own gem-server, I was able to solve this by writing the following in my Gemfile (the explicit version is crucial):
gem 'libv8', '3.11.8.3mytest', :path => '../libv8/pkg'
And the ../libv8/pkg folder contains only the binary packaged gem libv8-3.11.8.3mytest-x86_64-linux.gem.
Hope this helps.
I don't think you can. As far as I know, you need to gem unpack the .gem into something like vendor/ and set the :path option.
Can't you point the gem declaration to your ParseTree fork at Github?
I figured it out -- thanks to everyone who responded! :)
The trick was to set up a local gem server (with, uh, "gem server") and change my Gemfile's source to point to http://localhost:8808 instead of http://rubygems.org.
This means bundler will grab all the gems from the current installed gem set (which happens to be fine for my case) and then the compiled libs just work.

Resources