1. 创建下面的目录结构
myfirst_gem/
├── lib
│ └── myfirst_gem.rb
└── myfirst_gem.gemspec
2. 修改myfirst_gem.rb
class MyfirstGem
def self.hello_world
puts "Hello world!"
end
end
3. 修改myfirst_gem.gemspec
Gem::Specification.new do |s|
s.name = 'myfirst_gem'
s.version = '0.0.1'
s.date = '2016-02-24'
s.summary = "myfirst_gem"
s.description = "A simple hello world gem"
s.authors = ["eiffel_0311"]
s.email = 'eiffel_0311@163.om'
s.files = ["lib/myfirst_gem.rb"]
s.homepage ='http://rubygems.org/gems/myfirst_gem'
s.license = 'MIT'
end
4. 构建, 生成一个myfirst_gem-0.0.1.gem 的文件
gem build myfirst_gem.gemspec
5. 安装测试
gem install ./myfirst_gem-0.0.1.gem
打开irb:
require 'myfirst_gem'
myfirst_gem.hello_world
6. 查看远端,看是否上传成功:
另外: 可以编写测试脚本
目录结构:
myfirst_gem/
├── lib
│ └── myfirst_gem.rb
├── myfirst_gem-0.0.1.gem
├── myfirst_gem.gemspec
├── Rakefile.rb
└── test
└── test_myfirst_tem.rb
添加文件: rakefile.rb
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
end
desc "Run tests"
task :default => :test
添加test_myfirst_tem.rb
require 'minitest/autorun'
require 'myfirst_gem'
class MyfirstTest < Minitest::Test
def test_hello_world
assert_equal nil, MyfirstGem.hello_world
end
end
运行rake test