创建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
这样就可以创建和使用一个简单的Gem包了。更复杂的Gem包可能需要更多的配置和功能实现。