使用Bundler 开发 rubygem (Developing a RubyGem using Bundler)

发布时间: 2013-01-28 01:44:00

For English original post, please visit:

Bundler is a tool created by Carl Lerche, Yehuda Katz, André Arko and various superb contributors for managing Rubygems dependencies in Ruby libraries. Bundler 1.0 was released around the same time as Rails 3 and it's the Rails project where Bundler is probably most well-known usage occurs. But remember, Bundler isn't just for Rails! Did you know that you can use Bundler for not only gem dependency management but also for writing our own gems? It's really easy to do this and Bundler provides a couple of things to help you along this path.

1. bundle gem kaka

2. 在kaka.gemspec 文件中,加入:

s.add_development_dependency "rspec", "~> 2.6"  

3. bundle install ,就会有:

Using kaka (0.0.1) from source at /path/to/kaka

4. 实现一个 spec (很基础但是很重要:)

4.1 $ mkdir spec
4.2 $ vim file:  lib/kaka/dog.rb
module Kaka
    class Dog 
        def self.bark

4.3 $ vim lib/kaka.rb
+ require "kaka/dog"

4.4  $ vim spec/kaka_spec.rb
require 'kaka'
describe Kaka::Dog do
    it 'should pass' do
        Kaka::Dog.bark.should == "wangwang!"
    it 'should pluralize a word' do
        Kaka::Dog.pluralize("tomato").should eql("tomatoes")

4.5 $bundle exec rspec spec

5. 引用一个第三方的gem,例如为了使用pluralize方法,我们要使用ActiveSupport:

gem.add_dependency "activesupport", ">= 2.3.8"
gem.add_dependency "i18n"

6. $ bundle install

7. $ bundle exec rspec spec

8. $ rake build

kaka 0.0.1 built to pkg/kaka-0.0.1.gem

9. $ gem install pkg/kaka-0.0.1.gem