Reputation: 1263
I have a gem that I created in Ruby on my local machine and I need to require this gem in a plain Ruby script that starts a service.
I have to require like this:
require_relative '../../../my-gem/lib/my/gem'
Is it possible to do this require without putting in the relative path?
Upvotes: 3
Views: 4193
Reputation: 27845
If you have a gem, you can install it and set the version you want (in my example 1.0.0.beta) with gem 'my-gem', '= 1.0.0.beta'
.
But I think yu look for another solution:
You can extend the location where require
looks:
$:.unshift('../../../my-gem/lib')
require('my/gem')
or
$LOAD_PATH.unshift('../../../my-gem/lib')
require('my/gem')
You could also use $: << '../../../my-gem/lib'
, but I prefer unshift
. If your gem contains a file with similar names as in a gem (avoid it!), then unshift
guarantees your script is loaded.
Upvotes: 1
Reputation: 22315
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.
Upvotes: 2