From: Ryan Davis Date: 2006-08-07T07:19:06+09:00 Subject: Re: How to redirect a string to stdin within code? On Aug 6, 2006, at 2:59 PM, Christian Neukirchen wrote: > Alex Young 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. 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) from /usr/local/lib/ruby/1.8/irb/input-method.rb:97:in `gets' from /usr/local/lib/ruby/1.8/irb.rb:132:in `eval_input' from /usr/local/lib/ruby/1.8/irb.rb:259:in `signal_status' from /usr/local/lib/ruby/1.8/irb.rb:131:in `eval_input' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:189:in `buf_input' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:104:in `getc' from /usr/local/lib/ruby/1.8/irb/slex.rb:206:in `match_io' from /usr/local/lib/ruby/1.8/irb/slex.rb:76:in `match' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:287:in `token' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:263:in `lex' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:234:in `each_top_level_statement' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in `each_top_level_statement' from /usr/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in `each_top_level_statement' from /usr/local/lib/ruby/1.8/irb.rb:146:in `eval_input' from /usr/local/lib/ruby/1.8/irb.rb:70:in `start' from /usr/local/lib/ruby/1.8/irb.rb:69:in `start' from -e:1