當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Shellwords.shelljoin用法及代碼示例

本文簡要介紹ruby語言中 Shellwords.shelljoin 的用法。

用法

shelljoin(array)
也別名為:join

從參數列表 array 構建命令行字符串。

所有元素都連接到一個字符串中,字段由空格分隔,其中每個元素都為 Bourne shell 轉義並使用 to_s 進行字符串化。

ary = ["There's", "a", "time", "and", "place", "for", "everything"]
argv = Shellwords.join(ary)
argv #=> "There\\'s a time and place for everything"

Array#shelljoin 是此函數的快捷方式。

ary = ["Don't", "rock", "the", "boat"]
argv = ary.shelljoin
argv #=> "Don\\'t rock the boat"

您還可以在 Array#join 中允許的元素中混合非字符串對象。

output = `#{['ps', '-p', $$].shelljoin}`

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Shellwords.shelljoin。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。