# Bad file descriptor, but not on 2/3 computers

**URL:** <https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905>\
**Category:** Ruby\
**Created:** [March 29, 2010, 12:51am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905 "2010-03-29T00:51:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 12:51am UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/1 "2010-03-29T00:51:19Z")

</div>

I am running a Ruby program that takes files from the command line and  
then edits them based on user input. This part works perfectly fine.

The issue I am having is that when I pass an _input_ file I am getting  
an error:  
“in ‘gets’: Bad file descriptor (Errno::EBADF)”

I have tested this program on three machines, a windows laptop, a mac  
laptop, and a windows desktop.

The program runs perfectly on the two laptops, but not on my desktop.

The command line for the file looks like: Decrypt.rb test1.in test2.in  
\<input.in

I have no idea what that error is dealing with because the program runs  
perfectly on the two laptops.

Thank you,  
Tag

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/2 "2010-03-29T18:56:55Z")

</div>

> The issue I am having is that when I pass an _input_ file I am getting  
> an error:  
> “in ‘gets’: Bad file descriptor (Errno::EBADF)”

Could try other ruby versions.  
-rp

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/3 "2010-03-29T19:09:23Z")

</div>

Roger P. wrote:

> Could try other ruby versions.  
> -rp

Thank you Roger, I had forgot to mention that the program is part of a  
class and the online grading system only takes the ruby version I am  
using. (1.8.7)

All three computers use the same Ruby version.

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 7:14pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/4 "2010-03-29T19:14:29Z")

</div>

> “in ‘gets’: Bad file descriptor (Errno::EBADF)”  
> …  
> I have no idea what that error is dealing with because the program runs  
> perfectly on the two laptops.

I have seen something like this before, with 1.9…re-running the script  
(perhaps after a few second pause) fixed it there, and I was never able  
to come up with a reproducible test script to reproduce the problem. If  
you can then forward it on.  
-rp

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/5 "2010-03-29T19:17:37Z")

</div>

> I have seen something like this before, with 1.9…re-running the script  
> (perhaps after a few second pause) fixed it there, and I was never able  
> to come up with a reproducible test script to reproduce the problem. If  
> you can then forward it on.  
> -rp

To be honest I’m not precisely sure what you mean. This error occurs  
every time I run the program.

It takes command-line arguments: Decrypt.rb test1.in test2.in \< input.in

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 8:17pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/6 "2010-03-29T20:17:25Z")

</div>

> The program runs perfectly on the two laptops, but not on my desktop.
> 
> The command line for the file looks like: Decrypt.rb test1.in test2.in  
> \<input.in

That is indeed odd. You’d have to go down into the C to see why that is  
happening (and try to reproduce it with “straight C” or what not).

Perhaps it is caused by a subtle difference in OS or msvcrt.dll version?

Anyway, you could work around by noting that gets “by default” reads  
from the files passed as parameters:

[http://stackoverflow.com/questions/2294439/unexpected-gets-behavior-with-commandline-arguments-in-ruby-closed](http://stackoverflow.com/questions/2294439/unexpected-gets-behavior-with-commandline-arguments-in-ruby-closed)

But I’d probably do something like  
$ ruby Decrypt.rb test1.in test2.in input.in

file = File.open(ARGV.pop, ‘r’)  
file.gets

GL.  
-rp

---

<div class="post-metadata">

**Author:** ![kingtag1445](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@kingtag1445](https://www.ruby-forum.com/u/kingtag1445)\
**Post date:** [March 29, 2010, 8:19pm UTC](https://www.ruby-forum.com/t/bad-file-descriptor-but-not-on-2-3-computers/185905/7 "2010-03-29T20:19:14Z")

</div>

> But I’d probably do something like  
> $ ruby Decrypt.rb test1.in test2.in input.in
> 
> file = File.open(ARGV.pop, ‘r’)  
> file.gets
> 
> GL.  
> -rp

Thank you. 🙂
