# Redirect gets to get input from a file insted of from the us

**URL:** <https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664>\
**Category:** Ruby\
**Created:** [June 6, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664 "2009-06-06T13:57:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 6, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/1 "2009-06-06T13:57:24Z")

</div>

I have this small program, or something like it as this just bigger:

test.rb

def testing

t = gets  
puts t

k = gets  
puts k

end

test2.rb

testing

When I run test2.rb it asks for user input. But I want the program to  
get this input from a file instead of asking the user. And I do not want  
to change the code in test.rb. I can only change the code in test2.rb or  
make new files, but I cant change test.rb

I only now how to do this by running the program from the console

ruby test2.rb \< input.txt

But that is not what I want. I want to do this inside the program. I do  
not want the user to start it in the console like that. I want the user  
only to start the program an th program get the input from the file (or  
a text string in the program code) it self.

I am new to ruby, and new to this kind of programming, so please don’t  
kill me if it is a stupid question.

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 6, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/2 "2009-06-06T14:14:25Z")

</div>

On Sat, Jun 6, 2009 at 7:57 AM, Kga A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> test2.rb
> 
> testing

This works:

def testing  
t = gets  
puts t

k = gets  
puts k

end

require ‘stringio’

$stdin = StringIO.new(“abc\ndef\n”)

testing

Of course you’d want to set $stdin to the open file.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 9, 2009, 11:32am UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/3 "2009-06-09T11:32:10Z")

</div>

I have testet it om some programms, and it gets most outputs. But thee  
is some output to the console i havent managed to catch. Might it be a  
non standard output? How do i catch it? Is thee som way to catch  
everything that are written to det consol, and not only the standard  
output ?

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 9, 2009, 12:28pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/4 "2009-06-09T12:28:33Z")

</div>

Kga A. wrote:

> I have testet it om some programms, and it gets most outputs. But thee  
> is some output to the console i havent managed to catch. Might it be a  
> non standard output? How do i catch it? Is thee som way to catch  
> everything that are written to det consol, and not only the standard  
> output ?

Ops, i asked for input earlier. But now i’m after output. I want to  
collect all kind of outputs that the program send to the commandline. I  
now how to catch most of it by using somthing simmilar to the  
description above, only with $stdout insted. But it dont catch  
everything. There is some outputs to the command line it dont catch. I  
dont now what kind of output it is, but it is a error message and  
printet in red in eclipse

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 6, 2009, 6:59pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/5 "2009-06-06T18:59:40Z")

</div>

Rick Denatale wrote:

> On Sat, Jun 6, 2009 at 7:57 AM, Kga A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > test2.rb
> > 
> > testing
> 
> This works:
> 
> def testing  
> t = gets  
> puts t
> 
> k = gets  
> puts k
> 
> end
> 
> require ‘stringio’
> 
> $stdin = StringIO.new(“abc\ndef\n”)
> 
> testing
> 
> Of course you’d want to set $stdin to the open file.

A bit of care is needed, because if the program is given command-line  
arguments, Kernel#gets will read from the files listed on the  
command-line (in the same way as ARGF.gets, I believe)

If test.rb really wants to read from stdin, it should do

```
$stdin.gets

```

Of course, you already said you couldn’t modify test.rb. So perhaps you  
need to do ARGV.clear or something like that.

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 9, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/redirect-gets-to-get-input-from-a-file-insted-of-from-the-us/169664/6 "2009-06-09T13:55:31Z")

</div>

Kga A. wrote:

> Kga A. wrote:
> 
> > I have testet it om some programms, and it gets most outputs. But thee  
> > is some output to the console i havent managed to catch. Might it be a  
> > non standard output? How do i catch it? Is thee som way to catch  
> > everything that are written to det consol, and not only the standard  
> > output ?
> 
> Ops, i asked for input earlier. But now i’m after output. I want to  
> collect all kind of outputs that the program send to the commandline. I  
> now how to catch most of it by using somthing simmilar to the  
> description above, only with $stdout insted. But it dont catch  
> everything. There is some outputs to the command line it dont catch. I  
> dont now what kind of output it is, but it is a error message and  
> printet in red in eclipse

Hehe, i’m a clutterhead. It was just an stderr output.
