Ruby equivalent of Python's subprocess.check_call/check_output -


python provides 2 convenient functions calling subprocesses might fail, subprocess.check_call , subprocess.check_output. basically,

subprocess.check_call(['command', 'arg1', ...]) 

spawns specified command subprocess, blocks, , verifies subprocess terminated successfully (returned zero). if not, throws exception. check_output same thing, except captures subprocess's stdout , returns byte-string.

this convenient because single python expression (you don't have set , control subprocess on several lines of code), , there's no risk of forgetting check return value.

what idiomatic ruby equivalents check_call , check_output? aware of $? global gives process's return value, awkward—the point of having exceptions don't have manually check error codes. there numerous ways spawn subprocess in ruby, don't see provide feature.

it's hard what's idiomatic solution in ruby… but 1 that's closest python shell.execute! shell-executer.

from example on docs page:

begin   shell.execute!('ls /not_existing') rescue runtimeerror => e   print e.message end 

compare to:

try:     subprocess.check_call('ls /not_existing', shell=true) except exception e:     print e.message 

the notable difference here ruby equivalent doesn't have way shell=false (and take args list), python not has, defaults to.

also, python's e.message default message or generated based on return code, while ruby's e.message child's stderr.

if want shell=false, far know, you'll have write own wrapper around lower level; of ruby wrappers know of (shell-executer, popen4, [open4][4]) wrappers around or emulators of posix popen functions.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -