From: Robert Dober Date: 2006-08-06T16:47:38+09:00 Subject: Re: How to redirect a string to stdin within code? ------=_Part_118340_11242245.1154850455553 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/6/06, Alex Young wrote: > > Logan Capaldo wrote: > > > > 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 =3D "myname" > >> $stdin =3D input > >> start my class that I would like to test > >> > >> Any help is greatly appreciated. > >> > >> Christian > >> > > > > require 'stringio' > > > > input =3D StringIO.new("myname") > > > > $stdin =3D input > But: > > alex@pandora:~$ irb > irb(main):001:0> require 'stringio' > =3D> true > irb(main):002:0> input =3D StringIO.new("myname") > =3D> # > irb(main):003:0> $stdin =3D input > =3D> # > /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' =3D> true irb(main):002:0> input =3D StringIO.new("myname") =3D> # irb(main):003:0> $stdin =3D input =3D> # irb(main):004:0> irb(main):004:0> NameError: undefined local variable or method `myname' for main:Object from (irb):4 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] robert@:~/log/ruby/tests 09:45:20 512/14 > irb --version irb 0.9.5(05/04/13) robert@:~/log/ruby/tes Cheers Robert -- > Alex > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_118340_11242245.1154850455553--