I have git cloned a repo from Github, now I want to experiment with it, as in I want to poke around the code and mess with it. I’ve created a file test.rb that should load this gem, but I want to load my locally checked out version, what’s the right way to do this?
Right now I’m just using a bunch of "require_relative 'the_gem_name/lib/file'", which feels wrong.
When you
require 'foo'Ruby checks all the directories in the load path for a filefoo.rband loads the first one it finds. If no file namedfoo.rbis found, and you’re not using Rubygems, aLoadErroris raised.If you are using Rubygems (which is likely given that it is included in Ruby 1.9+), then instead of immediately raising a
LoadErrorall the installed Gems are searched to see if one contains a filefoo.rb. If such a Gem is found, then it is added to the load path and the file is loaded.You can manipulate the load path yourself if you want to ensure a particular version of a library is used. Normally this isn’t something that’s recommended, but this is the kind of situation that you’d want to do it.
There are two ways of adding directories to the load path. First you can do it in the actual code, using the
$LOAD_PATH(or$:) global variable:Note that you normally want to add the
libdir of the gem, not the top level dir of the gem (actually this can vary depending on the actual Gem, and it’s possible to need to add more than one dir, butlibis the norm).The other way is to use the
-Icommand line switch to therubyexecutable:This way might be a bit cleaner, as normally you don’t want to be messing with the load path from inside your code, but if you’re just testing the library it probably doesn’t matter much.