Modify a gem and use it in Rails

Swapnil Gourshete | June 28, 2019 | 0 Minute Read

Sometimes a situation arises where we need to modify a gem according to our requirement and then successfully integrate into our project. Let’s find out!

1. Download the repository of required gem from github.

2. Open codebase in any of the editor. Play with it(Rather make meaningful changes!).

3. Build gem locally. This is the most important STEP!!!
Run `gem build foo.gemspec`
This will create foo.gem file in the same directory. Remember the path of this directory.

4. In Gemfile put it down like
gem 'foo', path: 'path/to/above/directory'

5. Run `bundle install`

And amigo you will be using the modified gem version.


This setup will be helpful, mostly for try and test approach.
For a slightly better approach you can use github.

To use github, remember, the git source of gem must be specified.

3. In Gemfile put it down like
gem 'foo', github: 'path/to/repository/relative/to/github', branch: 'your_branch'

4. Run `bundle install`

And you are off to the mark.