2

正如标题所述,我是 Ruby 的初学者。

我的项目使用 2 个现有的 gem - 我想修改它们。我已经在 GitHub 上分叉了这些 gem,并将它们作为模块放在我的 repo 中,它们显示为一个子目录。

我试过研究这个,但我一直迷路 - 我想我在这里遗漏了一些基本概念/知识。

我的问题:

  1. 我在做这件事对/错吗?
  2. 是否可以在我的实际项目中包含那些(分叉的)gem 的代码,或者我应该单独更新它们并将它们用作实际的 gem require(这看起来很麻烦)
  3. 如果这些宝石是我项目的一部分,我该如何正确使用它们,我假设我不需要这require件作品?如果没有,我如何访问/使用它们?

谢谢!

顺便说一句,在 Ubuntu 上使用 Ruby 1.9.2-p194 和 RubyMine 作为 IDE。

4

1 回答 1

2
  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.

于 2012-07-29T00:08:47.600 回答