Script RC on Windows

I have another application that allows data to be passed through filters
written in any normal language or scripting language. Included in its
distribution are sample filters written in a variety of languages
showing how trivial a filter can actually be. The demo filters all
perform a very basic function - uppercase the data stream. I have demo
filters in C, CScript, Perl, etc. all equally trivial, all working fine.
So I thought I’d add a Ruby version.

I know almost nothing about Ruby, but coded the following small demo:

while line = gets do
line = line.upcase
puts line
end
exit 0

But running this via a shell to Windows from my application always
returns with a reported return code of 1 (treated as an error). It is
called as - example.rb output file

What am I overlooking in order to get this script to return with zero?

It runs fine in a DOS command window, but then you don’t get to see the
RC there. Do I need to to something specific to use stdin/stdout?

I’m not completely sure what is wrong with your code. I do not have a
Windows machine at the moment, but I can give you some pointers that may
help.

First of all, the exit 0 at the end is redundant. If there are no
errors, then the Ruby interpreter will automatically exit with a code of
zero(0).

The line = line.upcase is also redundant(and ugly). It would look
prettier if you just did that all on one line(puts line.upcase).

Also, when interpreting a Ruby file, you need to invoke the Ruby
interpreter. This means calling ruby, then the file name and
arguments. That means you would call your script as, ruby example.rb <input file> <output file>.

Hi George,

You mean like this?
ruby example.rb stdout

Your code finished on my Windows(XP) too.
But, RC was 0.
I tested.

  • ruby 1.9.3p125 (2012-02-16) [i386-mingw32]
  • ruby 1.8.7 (2012-02-08 patchlevel 358) [i386-mingw32]

Please write full of your commands.
I guess, your command is below.
ruby example.rb stdout & echo %ERRORLEVEL%

This is not same with below command on BourneShell.
ruby example.rb stdout; echo $?

Try this.
ruby example.rb stdout
echo %ERRORLEVEL%

This problem from “cmd.exe”.
(solved in “cmd.exe /v:on” and “setlocal enabledelayedexpansion”)

And your code is able to change below, in this case.(better for me)

STDIN.each do |line|
  print line.upcase
end

or

while line = STDIN.gets
  print line.upcase
end

“Kernel#gets” connect to “ARGF” that is STDIN and more.

On Tue, Feb 28, 2012 at 8:58 PM, Kenichi K. [email protected]
wrote:

  • ruby 1.8.7 (2012-02-08 patchlevel 358) [i386-mingw32]
    echo %ERRORLEVEL%
    or

while line = STDIN.gets
print line.upcase
end

“Kernel#gets” connect to “ARGF” that is STDIN and more.

Kenichi, I keep seeing your messages here, but I never know what
you’re responding to. Please quote at least part of the original
messages.

Eric

Sorry, I didn’t use “Reply with quote” on ruby-forum.

Eric C. wrote in post #1049385:

On Tue, Feb 28, 2012 at 8:58 PM, Kenichi K. [email protected]

Kenichi, I keep seeing your messages here, but I never know what
you’re responding to. Please quote at least part of the original
messages.

On Tue, Feb 28, 2012 at 10:52 PM, Eric C.
[email protected] wrote:

messages.

Sorry if I came off rude. I just discovered another part of why I
didn’t know what you were replying to – I’m not getting all (or any?)
the ruby-forum messages here on ruby-talk.

Oh, obviously I am getting some ruby-forum messages, since I get
Kenichi’s.

Does anyone know what’s going on with the relay?

Hi Everyone,
Thanks to all of you who responded, especially with the much simpler
version of the script.

I found my problem and it has nothing to do with Ruby, but with extreme
stupidity on my part. I’d explain, but it’s embarrassing, trust me, a
truly stupid brain lapse.

And if you let the Ruby installer set the file association, the command
only needs to be: Example.rb output you don’t need to specify
Ruby.exe.

Thanks again, sorry for a dumb question.

George

On Tue, Feb 28, 2012 at 9:21 PM, Kenichi K. [email protected]
wrote:

Eric

Sorry, I didn’t use “Reply with quote” on ruby-forum.

Eric C. wrote in post #1049385:

On Tue, Feb 28, 2012 at 8:58 PM, Kenichi K. [email protected]

Kenichi, I keep seeing your messages here, but I never know what
you’re responding to. Please quote at least part of the original
messages.

Sorry if I came off rude. I just discovered another part of why I
didn’t know what you were replying to – I’m not getting all (or any?)
the ruby-forum messages here on ruby-talk.