dogwood008の開発メモ!

最近のマイブームは機械学習, Ruby on Rails。中でも機械学習を使った金融商品の自動取引に興味があります。

Rubyスクリプト内からシェル実行するときに、クォートの面倒な処理を全部引き受けてくれるモジュール「Shellwords」

f:id:dogwood008:20151106000435p:plain image by Tom Schaub

ongaeshi.hatenablog.com

詳しい話はid:tuto0621さんのブログに譲るとして、10秒で何ができるかを説明する。

こんな感じで使える。

require 'shellwords'

system("ls #{Shellwords.escape(ARGV[0])}")

こんな仕組み。

irb(main):013:0> Shellwords.escape("foo bar.txt")
=> "foo\\ bar.txt"
irb(main):014:0> Shellwords.escape("a+b+c bar.txt")
=> "a\\+b\\+c\\ bar.txt"

便利。