創(chuàng)建Gem包:
$ mkdir my_gem
$ cd my_gem
$ touch my_gem.gemspec
$ mkdir lib
$ touch lib/my_gem.rb
$ touch README.md
my_gem.gemspec
文件,指定Gem包的信息和依賴:Gem::Specification.new do |spec|
spec.name = "my_gem"
spec.version = "0.1.0"
spec.authors = ["Your Name"]
spec.summary = "A brief description of my_gem"
spec.description = "A longer description of my_gem"
spec.email = "youremail@example.com"
spec.files = Dir["lib/**/*.rb"]
spec.add_runtime_dependency "other_gem", "~> 1.0"
end
lib/my_gem.rb
文件中定義你的Gem包功能:module MyGem
def self.say_hello
puts "Hello, world!"
end
end
$ gem build my_gem.gemspec
$ gem install my_gem-0.1.0.gem
使用Gem包:
require 'my_gem'
MyGem.say_hello
這樣就可以創(chuàng)建和使用一個(gè)簡(jiǎn)單的Gem包了。更復(fù)雜的Gem包可能需要更多的配置和功能實(shí)現(xiàn)。