Ruby Noob: Using stderr, stdout, etc

I’m a QA tester currently using ruby/watir for testing web sites/
applications. I’ve done fairly well so far for our small needs -
automating IE browsing/tasks, assertion results logging (custom
logger), and e-mail notifications for assertion errors.

I would like to log the console output from the ruby program (shown in
either DOS or SciTE) to a file just to have, but I’m not sure if this
is what stderr/stdout is talking about. It seems to be such a well-
known concept that nobody really talks about it in detail on the web.
I know what they are by definition, but beyond that I’m having trouble
finding anything.

Any help/information you can provide is greatly appreciated.

Thanks –

On Fri, Mar 02, 2007 at 06:40:10AM +0900, [email protected] wrote:

I would like to log the console output from the ruby program (shown in
either DOS or SciTE) to a file just to have, but I’m not sure if this
is what stderr/stdout is talking about. It seems to be such a well-
known concept that nobody really talks about it in detail on the web.
I know what they are by definition, but beyond that I’m having trouble
finding anything.

What platform are you running under - e.g. Windows or Unix?

If it’s Unix, then stdin and stdout are well-defined and
well-documented. If
it’s Windows, then I’m not sure that they are :slight_smile:

On Mar 1, 5:38 pm, Brian C. [email protected] wrote:

If it’s Unix, then stdin and stdout are well-defined and well-documented. If
it’s Windows, then I’m not sure that they are :slight_smile:

Windows :smiley:

Thanks for the explanation Tim, that’s exactly what I needed. I
didn’t realize they would act differently under windows/unix - I think
I was stuck on imagining them as code-specific items because I wasn’t
used to hearing about them in unix.

Strangely enough, I’m ok as far as unix and pipe/redirection, and I’ve
been logging output of standard unix stuff to files for a couple of
years - just never knew specifically what the output was called. Go
figure.

It seems to be such a well-
known concept that nobody really talks about it in detail on the web.
I know what they are by definition, but beyond that I’m having trouble
finding anything.

Hm. Under Unix, processes automatically have three streams/files
attached to them:

“At program startup, three streams are predefined and need not be
opened explicitly: standard input (for reading conventional input),
standard output (for writing conventional output) and standard error
(for writing diagnostic output).”
(stdin)

By default, stdin is the input coming from the keyboard and stderr and
stdout are output to the terminal the process is running in. This can
be manipulated, though. For example if you want the output of a script
myprog.rb to be written into a file named output rather than on
the terminal, you redirect the output like this:

$ myprog.rb > output

Under Unix (you’ll need to experiment with windows) this only
redirects stdin, which is useful, because you still get to see error
messages and they won’t get mixed in with the results of the program.

stdout is also where everything you write with puts and printf ends
up.

Google for something like “unix beginner’s tutorial” and search around
in there for terms like pipe and redirection
-tim

On 3/2/07, Jenda K. [email protected] wrote:

API call to execute the script using the file extension mapping than
what it would use for .exe and thatthe former doesn’t preserve the
STDxxx redirection.

Also, although I haven’t really kept up with windows, it used to be
able to redirect stdout, but not stderr.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

[email protected] wrote:

Thanks for the explanation Tim, that’s exactly what I needed. I
didn’t realize they would act differently under windows/unix - I think
I was stuck on imagining them as code-specific items because I wasn’t
used to hearing about them in unix.

Strangely enough, I’m ok as far as unix and pipe/redirection, and I’ve
been logging output of standard unix stuff to files for a couple of
years - just never knew specifically what the output was called. Go
figure.

The thing to watch out for under Windows is that (at least on some
versions)

script.rb > results.txt

doesn’t work and you have to write

ruby script.rb > results.txt

This is caused by the fact that the cmd.exe has to use a different WIN32
API call to execute the script using the file extension mapping than
what it would use for .exe and thatthe former doesn’t preserve the
STDxxx redirection.

On 3/2/07, Rick DeNatale [email protected] wrote:

figure.
This is caused by the fact that the cmd.exe has to use a different WIN32
API call to execute the script using the file extension mapping than
what it would use for .exe and thatthe former doesn’t preserve the
STDxxx redirection.

Also, although I haven’t really kept up with windows, it used to be
able to redirect stdout, but not stderr.

I was surprised when I learnt that since win2k it’s possible to
redirect stderr and even redirect stderr to stdout or vice versa,
using

progname 2> stderr.txt and progname 2&>1

You know, after all those years, maybe we’ll be able to call cmd.exe a
shell :smiley:

Other useful bits are:

^ is escape char - for escaping |, > etc.
|| and && work in windows as well

So… reading help might be useful and surprising sometimes…