From: Ronald Fischer Date: 2007-07-04T21:49:09+09:00 Subject: Re: Feature Request: Special file '-' denoting stdin/stdout > IMHO the question is whether a string is the proper interface to a > method. Agreed. I think the problem originates from the the Unix/Linux/Solaris world, where it is often not possibile to specify stdin/stdout/stderr in the same way as a filename (some however *do*, AFIK, by allowing something like "/dev/fd0" to be used as a "filename" denoting stdin). > Consider this: > > def write_stuff_file(out) > File.open(out, "w") do |io| > io << ... > end > end > > vs. > > def write_stuff_stream(out) > out << ... > end > > The second form is more flexible than the first one because you can > pass not only file names but IO objects, Strings, StringIOs etc. And > the second method does not need the "-" hack because you can just pass > $stdout. I would rarely use the "-" convention within my program; only as a way to receive this uniformly from the outside. And, yes, I'm not that much satisfied with using just a dash to denote it. It only happened to be used that way so often, so people are likely familiar with it. > > Also, when using ARGF for reading then you automatically get input > from stdin if there are no arguments in ARGV. This is another > situation where you do not need "-" as representative for stdout or > stdin. If it were just for this case, we would for sure not need a "-". But consider the Linux cat command, which takes an arbitrary number of filenames, concatenates their content and writes it to stdout. Here an example invocation of cat: middle|cat foo - bar >baz This first copies the content of file foo to baz, then appends the output of the process "middle" to baz, and finally appends file bar to baz. From the user interface point of view, you need some way here to represent stdin on the command line, and by convention this is done by cat. If you implement this in Ruby, you certainly *have* to deal with "-" explicitly, for example along the way Erik Veenstra proposed. I use for instance in my code calls like this: f = ARGV[n]=="-" ? $stdin : File.open(ARGV[n]) to implement this behaviour. This for sure can be done explicitly, but if things are done over and over in the same way by different teams of programmers, it makes sense to make a general solution from it. And, as some people in this list pointed out, maybe it would be really best to provide such a feature as gem and see whether it becomes popular. I will see whether I can do it (as I have never implemented a Gem so far). Ronald