2012-07-02 30 views
-1

我對capistrano很新穎。我想知道如何在capistrano任務中對變量進行子串處理。Capistrano任務內的子字符串的使用

雖然這給了我IRB內會發生什麼

ruby-1.9.2-p136 :012 > release_path = "12345678910" 
=> "12345678910" 
ruby-1.9.2-p136 :019 > release_path[-6..-1] 
=> "678910" 

什麼都不做一個Capistrano的任務中

namespace :namespacename do 
    task :taskname do 

    release_path = "1234678910" 
    release_path[-6..-1] 

    # output is still "12345678910" 
    puts release_path 

    end 
end 

任何人如何做變量使用Ruby類/方法中capistrano任務?提前致謝。

回答

1

它在Capistrano的所有紅寶石,所以,只要過得真:

namespace :namespacename do 
    task :taskname do 

    release_path = "1234678910" 
    release_path[-6..-1]  #<---- NO!!! 

    # output is "678910" 
    puts release_path[-6..-1]  #<---- YEAH BOY!!! 

    release_path = release_path[-6..-1] 
    puts release_path  # output is "678910" 

    release_path[-3..-1] # does nothing because "910" is returned into thin air 
    puts release_path[-3..-1]  # output is "910" 
    puts release_path[-3..-1][-2..-1] # output is substring of substring "10" 
    end 
end 

使用子範圍語法[X ... Y]它會返回它,不會截斷它,並存儲在同一個變量。

HTH

+0

首先感謝您的回答。如果我通過使用_puts release_path [-6 ..- 1]的代碼註釋正確地得到它,應該做到這一點,對吧?嘗試了這一點,不幸的是結果相同。 _release_path_仍包含原始值。 – felic

+0

如果你想使用release_path = release_path [-6 ..- 1]來釋放release_path到子字符串,因爲使用[]語法只返回它,很難解釋,所以我會用更多的例子更新我的答案 –

+0

The因爲irb是非常詳細的,並告訴你每個命令是怎麼回事,無論它是否實際輸出,你在irb中看到輸出的原因是我已標記爲「輸出到空氣中」的東西 –

相關問題