How to reference a local gem from a ruby script?
Make sure that your gem name as same as in Gemfile (e.g. custom_gem
)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
Don't forget to actually install this gem using bundlerbundle install
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something
How can I specify a local gem in my Gemfile?
I believe you can do this:
gem "foo", path: "/path/to/foo"
Require local gem ruby
require
checks for files in $LOAD_PATH
. You can put your gem in one of those directories in order to require
it directly. If you don't like your load path, you can add a new directory to it in your script, or set the RUBYLIB
environment variable which is added to the load path.
How do I specify local .gem files in my Gemfile?
This isn't strictly an answer to your question about installing .gem
packages, but you can specify all kinds of locations on a gem-by-gem basis by editing your Gemfile.
Specifying a :path
attribute will install the gem from that path on your local machine.
gem "foreman", path: "/Users/pje/my_foreman_fork"
Alternately, specifying a :git
attribute will install the gem from a remote git repository.
gem "foreman", git: "git://github.com/pje/foreman.git"
# ...or at a specific SHA-1 ref
gem "foreman", git: "git://github.com/pje/foreman.git", ref: "bf648a070c"
# ...or branch
gem "foreman", git: "git://github.com/pje/foreman.git", branch: "jruby"
# ...or tag
gem "foreman", git: "git://github.com/pje/foreman.git", tag: "v0.45.0"
(As @JHurrah mentioned in his comment.) How to tell the Gem File to use a specific local copy of a gem
Try, in your Gemfile
:
gem "mygem", :path => "/MyPath/MyGem.gem"
Note that it's probably best to use a relative link in there, like:gem "mygem", :path => "vendor/MyPath/MyGem.gem"
Run gem from local source code
From the root directory of your gem, try this to execute lib/MyGem.rb
:
ruby -Ilib lib/MyGem.rb
or test your gem interactive:irb -Ilib
> require 'mygem'
true
Related Topics
How to Show Longer Traces in Rails Testcases
Difference Between Render and Yield in Rails
Using a Self-Signed Certificate
Sql Like Operator in Ruby on Rails
What Are Ruby's Numbered Global Variables
How to Remove Ruby on Rails 4 Beta
Fresh Ruby Gem from Bundler - Cannot Load My Version.Rb File
Rvm Finding System Gems' Executables
Git: Forcing Tests Before Pushing to Local or Remote Master
Get All Products of Category and Child Categories (Rails, Awesome_Nested_Set)
Gem Install Pg for Ruby on Rails
Skipping: Touch Associations When Saving an Activerecord Object
Ruby-Open3.Popen3/How to Print The Output
Giving an Example of a Cycle in a Directed Graph