首页 > 代码库 > how to build a ruby gem

how to build a ruby gem

how to build a ruby gem

 

1. first you will creata a user in rubygem.org (https://rubygems.org/) 

 

 

2. create the directory structure like this:

 

Java代码  收藏代码
  1. $ tree    
  2. .    
  3. ├── cc_hola.gemspec    
  4. └── lib    
  5.     └── cc_hola.rb    

  

   you can use any name but you must keep consistency

 

 

3. in your .gemspec file

 

Java代码  收藏代码
  1. Gem::Specification.new do |s|  
  2.   s.name        = ‘CcHola‘  
  3.   s.version     = ‘0.0.0‘  
  4.   s.date        = ‘2014-10-20‘  
  5.   s.summary     = "A ruby gem build test!"  
  6.   s.description = "A ruby gem build test!"  
  7.   s.authors     = ["cckkll"]  
  8.   s.email       = ‘237178842@qq.com‘  
  9.   s.files       = ["lib/cc_hola.rb"]  
  10.   s.homepage    = ‘https://github.com/chengyuanheng‘  
  11. end  

 

 

4. in your .rb file

 

Java代码  收藏代码
  1. class CcHola  
  2.   def self.hi  
  3.     puts "Hello World!"  
  4.   end  
  5. end  

 

 

5. compiled gem

 

Java代码  收藏代码
  1. $ gem build cc_hola.gemspec  
  2. Successfully built RubyGem  
  3. Name: CcHola  
  4. Version: 0.0.0  
  5. File: CcHola-0.0.0.gem  
  6.   
  7. $ gem install ccHola-0.0.0.gem  
  8. Successfully installed CcHola-0.0.0  
  9. <span>1 gem installed</span>   

 

 

 6. test your gem

 

Java代码  收藏代码
  1. $ irb  
  2. > require "cc_hola"  
  3. => true  
  4. > CcHola.hi  
  5. Hello World!  
  6. => nil  

 

 

7. release your gem

 

Java代码  收藏代码
  1. $ curl -u cckkll https://rubygems.org/api/v1/api_key.yaml >~/.gem/credentials  
  2. Enter host password for user ‘cckkll‘:  
  3.   
  4. $ gem push CcHola-0.0.0.gem   
  5. Pushing gem to https://rubygems.org...  
  6. Successfully registered gem: CcHola (0.0.0)  

 

 

   you will find it in your rubygems account and all people can use it by

 

Java代码  收藏代码
  1. gem ‘CcHola‘, ‘~> 0.0.0‘  

 

how to build a ruby gem