On May 22, 2006, at 12:00 PM, Geoff Stanley wrote:

> Ohad Lutzky wrote:
>> Is there any reason you specifically need IRB for this? Because I'd
>> definetely go ahead and use eval.
>
> The purpose of this plugin is to teach and to facilitate learning.
>
> Say I'm learning Ruby from someone through IRC. This IRB plugin allows
> the teacher to quickly write some ruby code into IRB and get the  
> output
> to make sure it's correct, then click a button or write a command that
> copies (part of) that IRB session into the IRC channel. Similarly for
> the student. It makes for one less terminal window and a whole lot  
> less
> copy and pasting.
>
> Eval doesn't cut it because it only works for one line. Besides,  
> irb is
> much more pretty and informative.
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

Well then short of scavenging thru the irb source picking out the  
bits you need, seems like your options are:

a) run the IRC client *inside* irb, rather than the other way around.
b) IO.popen. This is icky though.

Incidentally irb is written in ruby, and does use eval. It just  
happens to know just enough ruby to keep asking you for input when it  
should. Also the prettiness and informative-ness comes from ruby's  
instropective capabilties.

e.g.
% cat simple_irb.rb
#!/usr/bin/env ruby
$SAFE =  0
print "> "
while gets
begin
   puts eval($_).inspect
rescue Exception => ex
   puts ex
end
   print "> "
end

% ruby simple_irb.rb
 > 1 + 2
3
 > x
undefined local variable or method `x' for main:Object
 > x = "Hello"
"Hello"
 > puts x
Hello
nil
 > exit!

Here's a "solution" involving IO.popen:

% cat irb_remote.rb
puts "Welcome to my pseudo irb frontend. Good luck with this."

IO.popen('irb', 'r+') do |irb|
   while line = gets
     irb.puts line
     puts irb.gets
   end
end


% ruby irb_remote.rb
Welcome to my pseudo irb frontend. Good luck with this.
1 + 2
1 + 2

3
4 + 1


4 + 1

5
^C/usr/local/ruby/lib/ruby/1.8/irb.rb:240:in `write': Broken pipe  
(Errno::EPIPE)        from /usr/local/ruby/lib/ruby/1.8/irb.rb:240:in  
`signal_handle'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:66:in `start'
         from /usr/local/ruby/lib/ruby/1.8/irb/input-method.rb:49:in  
`gets'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:132:in `eval_input'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:259:in `signal_status'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:131:in `eval_input'
         from /usr/local/ruby/lib/ruby/1.8/irb/ruby-lex.rb:189:in  
`buf_input'
         from /usr/local/ruby/lib/ruby/1.8/irb/ruby-lex.rb:104:in `getc'
          ... 7 levels...
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:146:in `eval_input'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:70:in `start'
         from /usr/local/ruby/lib/ruby/1.8/irb.rb:69:in `start'
         from /usr/local/ruby/bin/irb:13
irb_remote.rb:4:in `gets': Interrupt
         from irb_remote.rb:4
         from irb_remote.rb:3

As you can see IO.popen is quite fun to get working the way you want  
it to. I think the best idea would be to reimplement irb.