Rob V
Rob V

Reputation: 51

Ruby beginner - using /modifying existing gems in single project

As the title states, I'm a beginner in Ruby.

My project uses 2 existing gems - which I want to modify. I've forked the gems on GitHub and put them as modules in my repo and they show up as a subdirectory.

I've tried researching this, but I keep on getting lost - I'm thinking I'm missing some basic concepts/knowledge here.

My questions:

  1. Am I going about this right/wrong?
  2. Is it even possible to include the code of those (forked) gems in my actual project, or should I update them separately and just use them as actual gems with require (this seems very cumbersome)
  3. If these gems are part of my project, how do I use them properly, I assume I don't need the require piece? If not, how do I access/use them?

Thanks!

BTW, using Ruby 1.9.2-p194 on Ubuntu with RubyMine as the IDE.

Upvotes: 2

Views: 247

Answers (1)

cjhveal
cjhveal

Reputation: 5791

  1. Probably wrong. Ruby is a very flexible language, and has what are called open classes. This means that you can open up and change classes at run-time. Doing this on an external library is called monkey patching. So instead of having to replicate all of the code you want to stay consistent, you can just modify the classes and override any methods you want. A simple example:

    class Fixnum
      def is_multiple_of_three?
        self % 3 == 0
      end
    end
    

    However, if the changes you want are really significant, it could make sense to fork the gem.

  2. I recommend the bundler gem. This will let you make a Gemfile which lists all of your dependencies. You can list a github repository as a source for the gem, like so:

    gem 'gem_name_here', :git => 'git://github.com/username_here/gem_name_here.git'

    and then run bundle install to install your dependencies.

  3. If you install the gems with bundler, it acts just like any other gem you have installed.

Upvotes: 2

Related Questions