# How to redirect a string to stdin within code?

**URL:** <https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672>\
**Category:** ruby-talk\
**Created:** [5 August 2006 22:00 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672 "2006-08-05T22:00:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Seifert](https://avatars.discourse-cdn.com/v4/letter/c/edb3f5/32.png) [@Christian\_Seifert](https://rubytalk.org/u/Christian_Seifert)\
**Post date:** [5 August 2006 22:00 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/1 "2006-08-05T22:00:13Z")

</div>

Hello all, I am trying to create a unit test for a class that reads input from stdin (ie. the user enters his name when starting the class). I would like to write a unit test that provides the input via a string. something like:

input = "myname"  
$stdin = input  
start my class that I would like to test

Any help is greatly appreciated.

Christian

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [5 August 2006 22:50 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/2 "2006-08-05T22:50:44Z")

</div>

require 'stringio'

input = StringIO.new("myname")

$stdin = input

> **···**
>
> On Aug 5, 2006, at 6:00 PM, Christian Seifert wrote:
> 
> > Hello all, I am trying to create a unit test for a class that reads input from stdin (ie. the user enters his name when starting the class). I would like to write a unit test that provides the input via a string. something like:
> > 
> > input = "myname"  
> > $stdin = input  
> > start my class that I would like to test
> > 
> > Any help is greatly appreciated.
> > 
> > Christian

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [6 August 2006 06:13 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/3 "2006-08-06T06:13:10Z")

</div>

Logan Capaldo wrote:

> > Hello all, I am trying to create a unit test for a class that reads input from stdin (ie. the user enters his name when starting the class). I would like to write a unit test that provides the input via a string. something like:
> > 
> > input = "myname"  
> > $stdin = input  
> > start my class that I would like to test
> > 
> > Any help is greatly appreciated.
> > 
> > Christian
> 
> require 'stringio'
> 
> input = StringIO.new("myname")
> 
> $stdin = input

But:

alex@pandora:~$ irb  
irb(main):001:0\> require 'stringio'  
=\> true  
irb(main):002:0\> input = StringIO.new("myname")  
=\> #\<StringIO:0xb7a42864\>  
irb(main):003:0\> $stdin = input  
=\> #\<StringIO:0xb7a42864\>  
/usr/lib/ruby/1.8/irb/input-method.rb:97: [BUG] Segmentation fault  
ruby 1.8.4 (2005-12-24) [i486-linux]

Aborted

This is on Ubuntu Breezy. Any idea why that happens?

> **···**
>
> > On Aug 5, 2006, at 6:00 PM, Christian Seifert wrote:
> 
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [6 August 2006 07:47 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/4 "2006-08-06T07:47:38Z")

</div>

> Logan Capaldo wrote:  
> \>  
> \>\> Hello all, I am trying to create a unit test for a class that reads  
> \>\> input from stdin (ie. the user enters his name when starting the  
> \>\> class). I would like to write a unit test that provides the input via  
> \>\> a string. something like:  
> \>\>  
> \>\> input = "myname"  
> \>\> $stdin = input  
> \>\> start my class that I would like to test  
> \>\>  
> \>\> Any help is greatly appreciated.  
> \>\>  
> \>\> Christian  
> \>\>  
> \>  
> \> require 'stringio'  
> \>  
> \> input = StringIO.new("myname")  
> \>  
> \> $stdin = input  
> But:
> 
> alex@pandora:~$ irb  
> irb(main):001:0\> require 'stringio'  
> =\> true  
> irb(main):002:0\> input = StringIO.new("myname")  
> =\> #\<StringIO:0xb7a42864\>  
> irb(main):003:0\> $stdin = input  
> =\> #\<StringIO:0xb7a42864\>  
> /usr/lib/ruby/1.8/irb/input-method.rb:97: [BUG] Segmentation fault  
> ruby 1.8.4 (2005-12-24) [i486-linux]
> 
> Aborted
> 
> This is on Ubuntu Breezy. Any idea why that happens?

Very interesting I was just trying it on my Dapper and I get similar but  
not identical behavior, see below.  
However this seems an IRB issue Logans code just works fine in pure ruby

510/11 \> irb  
irb(main):001:0\> require 'stringio'  
=\> true  
irb(main):002:0\> input = StringIO.new("myname")  
=\> #\<StringIO:0xb7d2fe4c\>  
irb(main):003:0\> $stdin = input  
=\> #\<StringIO:0xb7d2fe4c\>  
irb(main):004:0\> irb(main):004:0\> NameError: undefined local variable or  
method `myname' for main:Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
irb(main):004:0\> robert@:~/log/ruby/tests 09:43:16

and

510/12 \> uname -a  
Linux roma 2.6.15-19-386 #1 PREEMPT Mon Mar 20 16:46:02 UTC 2006 i686  
GNU/Linux  
robert@:~/log/ruby/tests 09:45:04  
511/13 \> ruby --version  
ruby 1.8.4 (2005-12-24) [i686-linux]  
robert@:~/log/ruby/tests 09:45:20  
512/14 \> irb --version  
irb 0.9.5(05/04/13)  
robert@:~/log/ruby/tes

Cheers  
Robert

> **···**
>
> On 8/6/06, Alex Young \<alex@blackkettle.org\> wrote:
> 
> > \> On Aug 5, 2006, at 6:00 PM, Christian Seifert wrote:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0
> 
> --
> 
> > Alex
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Daniel\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Daniel\_Martin](https://rubytalk.org/u/Daniel_Martin)\
**Post date:** [7 August 2006 01:39 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/5 "2006-08-07T01:39:49Z")

</div>

Alex Young \<alex@blackkettle.org\> writes:

> But:
> 
> alex@pandora:~$ irb  
> irb(main):001:0\> require 'stringio'  
> =\> true  
> irb(main):002:0\> input = StringIO.new("myname")  
> =\> #\<StringIO:0xb7a42864\>  
> irb(main):003:0\> $stdin = input  
> =\> #\<StringIO:0xb7a42864\>  
> /usr/lib/ruby/1.8/irb/input-method.rb:97: [BUG] Segmentation fault  
> ruby 1.8.4 (2005-12-24) [i486-linux]
> 
> Aborted
> 
> This is on Ubuntu Breezy. Any idea why that happens?

Well, that particular line of the irb source is calling the method  
"readline" which is defined in readline.so - that is, in C. My guess  
is that it has some very specific assumptions about what type of  
object it's reading from. When I try that, I get:

esau:~$ irb  
irb(main):001:0\> require 'stringio'  
=\> true  
irb(main):002:0\> stream = StringIO.new("p [1,2,3,4]")  
=\> #\<StringIO:0xb7c9c734\>  
irb(main):003:0\> $stdin = stream  
=\> #\<StringIO:0xb7c9c734\>  
/usr/lib/ruby/1.8/irb/input-method.rb:97:in `readline': wrong argument  
type StringIO (expected File) (TypeError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/irb/input-method.rb:97:in `gets'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/irb.rb:132:in `eval\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/irb.rb:259:in `signal\_status'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/1.8/irb.rb:131:in `eval\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(many more lines of stacktrace omitted)

This is on my Debian testing system, so I should be running the same  
binary as you are, in theory:

esau:~$ ruby -v  
ruby 1.8.4 (2005-12-24) [i486-linux]

Note that "irb --noreadline" won't have this problem, but that has  
other issues (such as the fact that arrow keys don't work any more):

esau:~$ irb --noreadline  
irb(main):001:0\> require 'stringio'  
=\> true  
irb(main):002:0\> stream = StringIO.new("p [1,2,3,4]")  
=\> #\<StringIO:0xb7cdb7b8\>  
irb(main):003:0\> $stdin = stream  
=\> #\<StringIO:0xb7cdb7b8\>  
irb(main):004:0\> irb(main):004:0\> [1, 2, 3, 4]  
=\> nil  
irb(main):004:0\> esau:~$

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [6 August 2006 07:57 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/6 "2006-08-06T07:57:14Z")

</div>

Robert Dober wrote:  
\<snip\>

> > alex@pandora:~$ irb  
> > irb(main):001:0\> require 'stringio'  
> > =\> true  
> > irb(main):002:0\> input = StringIO.new("myname")  
> > =\> #\<StringIO:0xb7a42864\>  
> > irb(main):003:0\> $stdin = input  
> > =\> #\<StringIO:0xb7a42864\>  
> > /usr/lib/ruby/1.8/irb/input-method.rb:97: [BUG] Segmentation fault  
> > ruby 1.8.4 (2005-12-24) [i486-linux]
> > 
> > Aborted
> > 
> > This is on Ubuntu Breezy. Any idea why that happens?
> 
> Very interesting I was just trying it on my Dapper and I get similar but  
> not identical behavior, see below.  
> However this seems an IRB issue Logans code just works fine in pure ruby

Yes... I can see why it's not allowed in IRB (if you get rid of stdin, how the hell would you use the console? :-), I just don't know why it segfaults.

> 510/11 \> irb  
> irb(main):001:0\> require 'stringio'  
> =\> true  
> irb(main):002:0\> input = StringIO.new("myname")  
> =\> #\<StringIO:0xb7d2fe4c\>  
> irb(main):003:0\> $stdin = input  
> =\> #\<StringIO:0xb7d2fe4c\>  
> irb(main):004:0\> irb(main):004:0\> NameError: undefined local variable or  
> method `myname' for main:Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> irb(main):004:0\> robert@:~/log/ruby/tests 09:43:16
> 
> and
> 
> 510/12 \> uname -a  
> Linux roma 2.6.15-19-386 #1 PREEMPT Mon Mar 20 16:46:02 UTC 2006 i686  
> GNU/Linux  
> robert@:~/log/ruby/tests 09:45:04  
> 511/13 \> ruby --version  
> ruby 1.8.4 (2005-12-24) [i686-linux]

Interesting. In principle, we should get the same results - the only difference is the target processor. Allegedly.

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [6 August 2006 21:59 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/7 "2006-08-06T21:59:15Z")

</div>

Alex Young \<alex@blackkettle.org\> writes:

> > However this seems an IRB issue Logans code just works fine in pure ruby
> 
> Yes... I can see why it's not allowed in IRB (if you get rid of  
> stdin, how the hell would you use the console? :-), I just don't know  
> why it segfaults.

Just a guess: the readline extensions directly accesses $stdin without  
checking its type.

> **···**
>
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [6 August 2006 22:19 UTC](https://rubytalk.org/t/how-to-redirect-a-string-to-stdin-within-code/29672/8 "2006-08-06T22:19:06Z")

</div>

I think this is probably fixed in newer versions of ruby:

% ruby -v -rstringio -e 'log = StringIO.new("blah"); $stdin = log; require "irb"; IRB.start(\_\_FILE\_\_)'  
ruby 1.8.3 (2005-09-28) [i386-freebsd4.11]  
/usr/local/lib/ruby/1.8/irb/input-method.rb:97: [BUG] Segmentation fault  
ruby 1.8.3 (2005-09-28) [i386-freebsd4.11]

Abort trap (core dumped)

versus:

% ruby -v -rstringio -e 'log = StringIO.new("blah"); $stdin = log; require "irb"; IRB.start(\_\_FILE\_\_)'  
ruby 1.8.4 (2006-03-04) [i686-darwin8.6.2]  
Read 100 saved history commands from /Users/ryan/.irb.hist.  
Saving 100 history lines to /Users/ryan/.irb.hist.  
/usr/local/lib/ruby/1.8/irb/input-method.rb:97:in `readline': wrong argument type StringIO (expected File) (TypeError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/input-method.rb:97:in `gets'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:132:in `eval\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:259:in `signal\_status'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:131:in `eval\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:189:in `buf\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:104:in `getc'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/slex.rb:206:in `match\_io'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/slex.rb:76:in `match'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:287:in `token'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:263:in `lex'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:234:in `each\_top\_level\_statement'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in `each\_top\_level\_statement'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in `each\_top\_level\_statement'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:146:in `eval\_input'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:70:in `start'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/1.8/irb.rb:69:in `start'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from -e:1

> **···**
>
> On Aug 6, 2006, at 2:59 PM, Christian Neukirchen wrote:
> 
> > Alex Young \<alex@blackkettle.org\> writes:
> > 
> > > > However this seems an IRB issue Logans code just works fine in pure ruby
> > > 
> > > Yes... I can see why it's not allowed in IRB (if you get rid of  
> > > stdin, how the hell would you use the console? :-), I just don't know  
> > > why it segfaults.
> > 
> > Just a guess: the readline extensions directly accesses $stdin without  
> > checking its type.
