Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | CLI | Play Well with Others | Accessing Exit Codes of Other Commands

概要

書籍 Build Awesome Command-Line Applications in Ruby2

Play Well with Others

詳細

$? という名前はBashをやっている人には慣れ親しんだ変数名だが
その他人にはわかりにくい。
より人間に読みやすくするため、標準ライブラリの English を利用します。

サンプル

require 'English'

# 成功時
system('echo hoge')
puts $?.exitstatus
puts $CHILD_STATUS.exitstatus

# 失敗時
system('invalid_command')
puts $?.exitstatus
puts $CHILD_STATUS.exitstatus

出力

hoge
0
0
127
127