File.open error when variable is used

I have a situation in a ruby program where a filename is being passed as
an argument to a program and is being used in the program as follows:

File.open($fileName, “r”)

The file refuses to open for read and the program abends despite the
fileName having the exact correct value! I have tried to strip, chomp,
and clean the fileName in every possible conceivable way and yet the
only thing that works is this stupid fix which only servers to reassign
it! Does anyone understand this?

if $fileName == “myfile” then $fileName = “myfile” end

On Sat, Jul 21, 2012 at 3:50 PM, Wayne Mr. [email protected] wrote:

I have a situation in a ruby program where a filename is being passed as
an argument to a program and is being used in the program as follows:

File.open($fileName, “r”)

Is there a particular reason to use a global variable?

The file refuses to open for read and the program abends

Uh, “abends”? Probably a JCL error then. Check the floor for any
dropped punch cards :slight_smile:

Sigh. Anyway. Unlikely to be able to help without knowing the platform,
exact version of Ruby, the exact error message, and having a test
case to reproduce.

Hi,

Well, how are we supposed to help now? We neither know the code nor the
exact error message.

What do you get when writing

p $fileName

just before the File.open?

By the way, are you sure you want a global variable? Using this for a
simple file name looks like a bad idea to me. And you should apply the
Ruby convention for variable names, which is “file_name” and not
“fileName” like in Java.

Ruby 1.6, that’s really ancient. Isn’t it possible for you to update? I
wouldn’t be surprised if a 10 year old version had all kinds of bugs.

Apart from that, I can only repeat what I already said: Check the error
message and the value of $outFile just before the file is opened. Simply
point $stdout and $stderr to some file, so that all output and errors
are written to this file.

The code is running on:

  Aix 6100-06
  Ruby 1.6.6 (2001-12-26) [powerpc-aix4.3.3.0]

This is the parameter line in a ksh script calling the ruby program
called execluderow2.rb

excluderow2.rb INFILE=invrolla.txt,OUTFILE=invrollb.txt

This is the method which is called to parse the input parms. Without
the ‘Crazy’ fig shown below, the program stops. The error only happens
when executing from aix cron and therefore the error code (if one is
given)cannot be seen. If I add the crazy fig shown below, all the file
opens later in the code work with no issue or error.

def parseArgs args
args = args.split(",")
args.each {
|arg|
puts arg
keyword, value = arg.split("=")
case keyword
when “INFILE”
$inFile = value
when “OUTFILE”
$outFile = value
end
}

Crazy fix

if $inFile == “invrolla.txt” then $inFile = “invrolla.txt” end
if $outFile == “invrollb.txt” then $outFile = “invrollb.txt” end
end

This is one of the file opens later in the code. It won’t work without
the crazy fix.

outputFile = File.new($outFile, “w”)