From: Brian Candler Date: 2003-03-16T23:47:46+09:00 Subject: Re: mapping $? to text message On Sun, Mar 16, 2003 at 11:12:09AM +0900, Joel VanderWerf wrote: > How do you go from $? (which refers to nil or a Process::Status > instance) to a text message, sorta like perror(3) does? For me (running 1.6.8 under FreeBSD), $? is a Fixnum. It looks like it is for you too: > irb(main):001:0> system 'qwerty' > false > irb(main):002:0> p $? > 32512 (try 'p $?.class' to be entirely sure) Under Unix you have to split it into two parts: $? & 255 - 0 for normal exit, 127 for stopped, otherwise the signal number which caused it to die $? >> 8 - the exit status returned by the child (if it exited normally) Under FreeBSD the signal number is also OR'd with 128 if core was dumped. So in the above case that's "normal exit, status 127" (returned by the shell, which is a child process). For comparison, start a fresh session (so $? is nil), do 'exec "qwerty"', and you'll see $? is unchanged; it's only set when you wait(2) for a child. Now, there are POSIX macros for all this: see 'man 2 wait' and look in /usr/include/sys/wait.h It would be really nice if this were all wrapped in an object. I raised this a few weeks ago and didn't hear anything, so I guess it's not in 1.8. Actually I'd volunteer to wrap the Unix macros myself; but I know nothing about Windows (and I prefer to keep it that way). It would be great to have: p $?.exited? -- true p $?.exitstatus -- 127 Note that none of this has anything to do with perror(3), which takes the 'errno' value set by certain system calls, and turns it into a string. Ruby deals with 'errno' by raising exceptions, like Errno::ECONNREFUSED Regards, Brian.