From: Daniel Martin Date: 2006-08-07T10:39:49+09:00 Subject: Re: How to redirect a string to stdin within code? Alex Young writes: > But: > > alex@pandora:~$ irb > irb(main):001:0> require 'stringio' > => true > irb(main):002:0> input = StringIO.new("myname") > => # > irb(main):003:0> $stdin = input > => # > /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]") => # irb(main):003:0> $stdin = stream => # /usr/lib/ruby/1.8/irb/input-method.rb:97:in `readline': wrong argument type StringIO (expected File) (TypeError) from /usr/lib/ruby/1.8/irb/input-method.rb:97:in `gets' from /usr/lib/ruby/1.8/irb.rb:132:in `eval_input' from /usr/lib/ruby/1.8/irb.rb:259:in `signal_status' from /usr/lib/ruby/1.8/irb.rb:131:in `eval_input' (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]") => # irb(main):003:0> $stdin = stream => # irb(main):004:0> irb(main):004:0> [1, 2, 3, 4] => nil irb(main):004:0> esau:~$