# File.open error when variable is used

**URL:** <https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601>\
**Category:** Ruby\
**Created:** [July 22, 2012, 12:50am UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601 "2012-07-22T00:50:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 22, 2012, 12:50am UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601/1 "2012-07-22T00:50:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 22, 2012, 1:12am UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601/2 "2012-07-22T01:12:25Z")

</div>

On Sat, Jul 21, 2012 at 3:50 PM, Wayne Mr. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

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.

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 22, 2012, 1:15am UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601/3 "2012-07-22T01:15:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 22, 2012, 4:25pm UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601/4 "2012-07-22T16:25:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [July 22, 2012, 3:25pm UTC](https://www.ruby-forum.com/t/file-open-error-when-variable-is-used/221601/5 "2012-07-22T15:25:09Z")

</div>

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”)
