乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      如何創(chuàng)建自己的ruby gem包

       CJOL藏經(jīng)閣 2014-05-06


       編寫(xiě)一個(gè)最簡(jiǎn)單的例子

      1. 建好如下文件夾

      注意:lib目錄下必須有個(gè)和你gem名字一樣的rb文件。

      1. $ cd hola  
      2. $ tree  
      3. .  
      4. ├── hola.gemspec  
      5. └── lib  
      6.     └── hola.rb  


      2. 編寫(xiě)代碼

      . hola.rb

      1. % cat lib/hola.rb  
      2. class Hola  
      3.   def self.hi  
      4.     puts "Hello world!"  
      5.   end  
      6. end  

      .hola.gemspec

      1. % cat hola.gemspec  
      2. Gem::Specification.new do |s|  
      3.   s.name        = 'hola'  
      4.   s.version     = '0.0.0'  
      5.   s.date        = '2010-04-28'  
      6.   s.summary     = "Hola!"  
      7.   s.description = "A simple hello world gem"  
      8.   s.authors     = ["Nick Quaranto"]  
      9.   s.email       = 'nick@quaran.to'  
      10.   s.files       = ["lib/hola.rb"]  
      11.   s.homepage    =  
      12.     'http:///gems/hola'  
      13. end  
      這里面可以設(shè)置很多屬性。我會(huì)專門(mén)寫(xiě)篇文章介紹。

      上面字段的意思,比較簡(jiǎn)單。相信大家都能理解。


      3.編譯生成gem

      1. % gem build hola.gemspec  
      2. Successfully built RubyGem  
      3. Name: hola  
      4. Version: 0.0.0  
      5. File: hola-0.0.0.gem  
      6.   
      7. % gem install ./hola-0.0.0.gem  
      8. Successfully installed hola-0.0.0  
      9. 1 gem installed  

      4.測(cè)試使用

      1. % irb  
      2. >> require 'hola'  
      3. => true  
      4. >> Hola.hi  
      5. Hello world!  
      注意:在ruby 1.9.2之前到版本里面,需要先require 'rubygem',才能使用我們寫(xiě)的gem.


      5.發(fā)布到rubygems網(wǎng)站

      1. $ curl -u tom https:///api/v1/api_key.yaml >  
      2. ~/.gem/credentials  
      3. Enter host password for user 'tom':  
      設(shè)定完之后發(fā)布

      1. % gem push hola-0.0.0.gem  
      2. Pushing gem to RubyGems.org...  
      3. Successfully registered gem: hola (0.0.0)  
      發(fā)布成功。

      這樣任何一個(gè)人都可以使用你寫(xiě)的gem了。


      稍微復(fù)雜的rubygem例子

      上面的例子只有一個(gè)ruby文件,一般gem應(yīng)該沒(méi)有這么簡(jiǎn)單的。

      下面說(shuō)下有多個(gè)ruby文件該怎么寫(xiě)。

      1. 目錄結(jié)構(gòu)

      多了個(gè)hola目錄和translator.rb文件

      1. % tree  
      2. .  
      3. ├── hola.gemspec  
      4. └── lib  
      5.     ├── hola  
      6.     │   └── translator.rb  
      7.     └── hola.rb  

      2. 代碼

      lib/hola/translator.rb

      1. % cat  lib/hola/translator.rb  
      2. class Hola::Translator  
      3.   def initialize(language)  
      4.     @language = language  
      5.   end  
      6.   
      7.   def hi  
      8.     case @language  
      9.     when :spanish  
      10.       "hola mundo"  
      11.     else  
      12.       "hello world"  
      13.     end  
      14.   end  
      15. end  

      lib/hola.rb
      1. % cat lib/hola.rb  
      2. class Hola  
      3.   def self.hi(language = :english)  
      4.     translator = Translator.new(language)  
      5.     translator.hi  
      6.   end  
      7. end  
      8.   
      9. require 'hola/translator'  

      .hola.gemspec

      1. % cat hola.gemspec  
      2. Gem::Specification.new do |s|  
      3.   s.name        = 'hola'  
      4.   s.version     = '0.0.0'  
      5.   s.date        = '2010-04-28'  
      6.   s.summary     = "Hola!"  
      7.   s.description = "A simple hello world gem"  
      8.   s.authors     = ["Nick Quaranto"]  
      9.   s.email       = 'nick@quaran.to'  
      10.   s.files       = ["lib/hola.rb""lib/hola/translator.rb"]  
      11.   s.homepage    =  
      12.     'http:///gems/hola'  
      13. end  
      紅色是和上面不一樣的地方。


      其他步驟和上面一樣了。很簡(jiǎn)單吧!


      最后說(shuō)下怎么寫(xiě)個(gè) gem包含可執(zhí)行文件的例子。

      這個(gè)也很簡(jiǎn)單。像rake就是典型的包含可執(zhí)行文件的gem.

      1. 在剛才工程目錄下建個(gè)bin文件夾

      生成可執(zhí)行文件,并且修改權(quán)限為可運(yùn)行。

      1. % mkdir bin  
      2. % touch bin/hola  
      3. % chmod a+x bin/hola  

      2. 修改可執(zhí)行文件內(nèi)容
      bin/hola

      1. #!/usr/bin/env ruby   
      2.   
      3. require 'hola'  
      4. puts Hola.hi(ARGV[0])  

      測(cè)試下
      1. % ruby -Ilib ./bin/hola  
      2. hello world  
      3.   
      4. % ruby -Ilib ./bin/hola spanish  
      5. hola mundo  

      3 .最后修改gemspec
      1. % head -4 hola.gemspec  
      2. Gem::Specification.new do |s|  
      3.   s.name        = 'hola'  
      4.   s.version     = '0.0.1'  
      5.   s.executables << 'hola'  
      其他就和上面一樣了。很簡(jiǎn)單吧。








        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多