19

In Python I can install a package from source in "editable" mode using pip install -e. Then I can carry on editing the code, and any changes will be automatically picked by other Python scripts that import library

Is there a comparable workflow for developing Ruby gems? What is the "Ruby way" of using libs as they are being developed rather than, for example, compiling and installing a gem every time I make a change to the source?

3
  • 1
    If you just want to install a local gem gem install /path/to/gem or gem install --local path/to/gem should work for you. Nov 12, 2014 at 16:12
  • 1
    thanks. I will clarify the question. The point is I don't want to compile a gem in order to deploy the lib. I just want ruby to reference the source code directly so that I can informally test behavior as I am making changes. Nov 12, 2014 at 17:28
  • For this type of thing I would just use require rather than worrying about it as a gem. Nov 12, 2014 at 18:51

4 Answers 4

9
+50

There are two common approaches one might use with bundler:

  1. one executes bundle install --path vendor/bundle and does not run bundle update unless everything is tested.
  2. one tells a bundler to use a local version of the gem:
    • in Gemfile (this is not supported in mymaingem.gemspec due to rubygems maintainence issues): gem 'mycutegem', :git => 'git://github.com/myname/mycutegem', :branch => 'master';
    • in command line: bundle config local.mycutegem /path_to_local_git/mycutegem.

The first approach will download everything into subfolder of your current project (here it’d be vendor/bundle.) Feel free to modify everything there, it’ll be reflected.

The second approach is likely better. You are to clone the gem from github and instruct bundle to use your local clone of the respective git repository. This approach provides you with an ability to publish the changes to your main gem into the repository. As soon as dependent repo is published as well, the up-to-date version will be retrieven by your gem subscribers, assuming they have not instructed their bundlers to use their locals.

Hope this helps.

2
  • Why is this such a non-smooth task in Ruby? The second approach seems odd if you're working with someone else's code (e.g. if you don't want to edit the Gemfile). The first approach doesn't seem to work properly, it doesn't actually install anything except for dependencies (e.g. I can't run any commands from the gem). Apr 13, 2018 at 8:23
  • @fgblomqvist both approaches surely work but if you find it non-smooth, feel free to propose/implement your own; it’s open source after all. Apr 13, 2018 at 8:41
5

Let's assume you have your gem code residing in a folder (say my_project/mygem/lib). You have some Ruby code in my_project that you want using the mygem code.

What I'd do is add mygem/lib to the $LOAD_PATH global variable in the beginning of said files. Kinda like this:

$LOAD_PATH << File.expand_path('lib', './mygem') # Resolve global paths

require 'a_file' # Would require "mygem/lib/a_file.rb"
4
  • This is an answer to a question “how to entangle a bundler.” One should never use this approach due to it’s complete unmaintainability. May 14, 2015 at 9:59
  • @mudasobwa Care to elaborate? May 14, 2015 at 10:02
  • 1
    Local version becomes inconsistent with a remote one. git push and git pull from another machine would not pull the working copy of the project. Dependency management should be done by the tools, provided for that, not by sudden crazy programmer’s code changes. May 14, 2015 at 10:23
  • 1
    This solution is totally fine. Based on the author's context, it sounds like they are developing both gems in tandem, in which case, setting the load path yourself is perfectly reasonable. Using Bundler with the path is another reasonable option, it's just going to do the same thing. You don't deploy like this, it's for dev. I'd edit it to use File.expand_path, though. May 16, 2015 at 8:30
2

I am not sure if this is exactly what you want to achieve, but from the description I infer that you want to have a local copy of some gem and reference that gem in your project. If this is the case, you can (usually) achieve it in two steps:

  1. Clone the gem from VCS (in most cases: git), e.g. git clone url-of-the-gem-repo
  2. Reference the local copy using Bundler :path, e.g. gem "some-gem", :path => "/path/to/local/copy"

If the gem is stored at github, an even better way is to first fork it at github and then clone your own copy. Then, if you provide any improvements to the code in the local repo, you can easily share it with the world using a pull request.

0

I realize this is a 5 year old question, but I found all of these answers unsatisfying. Since I use ruby to develop CLI tools, using bundler for testing is not ideal. I need to be able to execute my test commands anywhere, and get the actual equivalent of pip install --editable.

Here's my solution.

  1. Use RVM to setup a new gemset for development

    rvm gemset use tools-dev --create

  2. Install your gems

    gem install mygem.gem

  3. Locate the installation directory

    gem list tool -d

  4. In the output, it will say installed at ${rvm_gemset_path}. Copy that path. It will also be an environment variable GEM_HOME, but I include this step for completeness and clarity.

  5. Delete the installed copy

    rm -Rf ${rvm_gemset_path}/gems/mygem-${version}

  6. Create a symlink to the git repository working directory.

    ln -s ${PWD} ${rvm_gemset_path}/gems/mygem-${version}

This bash script will do it in a more automated fashion assuming that you are in the git directory and you've set GEM_NAME as your gem's name.

rvm gemset use --create ${GEM_NAME}-dev
gem build ${GEM_NAME}.gemspec
gem install ${GEM_NAME}*.gem
gem_install_path=$(ruby -e "puts Gem::Specification.find_by_name('${GEM_NAME}').full_gem_path")
mv $gem_install_path "${gem_install_path}.bak"
ln -s $PWD $gem_install_path

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.