In Ruby, I'd like to access the status of the most recently executed external command's result information, that is, the information provided by $?
, but would prefer a named method or variable. Is there such a thing, and, if so, how do I access it?
CodePudding user response:
Process.last_status
returns the same Process::Status instance as $?
:
$ irb
> `ls 12341234`
ls: 12341234: No such file or directory
=> ""
> $? == Process.last_status
=> true
> $?.equal? Process.last_status
=> true
> $?.class
=> Process::Status
Thread Local
Also, this variable is thread local, so each thread will have its own variable (i.e. threads will not overwrite a single global value). From the docs at https://ruby-doc.org/core-2.5.0/Process.html#method-c-last_status:
"Returns the status of the last executed child process in the current thread."
CodePudding user response:
would prefer a named […] variable.
The English
standard library provides descriptive aliases for $?
and other special global variables. But unlike Process.last_status
this library has be loaded before any of its aliases can be used. The upside is it exists in Ruby < 2.5.
$ irb -rEnglish
> `ls 12341234`
ls: 12341234: No such file or directory
=> ""
> $? == CHILD_STATUS
=> true
> $?.equal? CHILD_STATUS
=> true
> $?.class
=> Process::Status