Joel VanderWerf
8/22/2007 1:14:00 AM
Shane Liesegang wrote:
> Can anybody point me to some good documentation (either online or
> dead-tree) of the IRB module? The RDoc is pretty bare-bones, and the
> samples I've found online aren't terribly well-commented or
> self-explanatory.
>
> As background: I'm trying to hook up the text-rendering of my OpenGL
> engine to act as a Ruby console. I've got it interpreting individual
> lines with eval(), but I'd like to set it up with IRB proper to make use
> of the history, prompting, output, etc.
This may not be quite what you're looking for, but whenever I want to
add an irb-like shell to a command-line application, I start by mucking
around with the snippet below (note the difference in the three ways of
invoking irb). Setting the prompt is easier; look for IRB.conf[:PROMPT]
in the irb source.
#!/usr/bin/env ruby
require 'irb'
require 'irb/completion'
module IRB
def IRB.parse_opts
# Don't touch ARGV, which belongs to the app which called this module.
end
def IRB.start_session(*args)
unless $irb
IRB.setup nil
## maybe set some opts here, as in parse_opts in irb/init.rb?
end
workspace = WorkSpace.new(*args)
if @CONF[:SCRIPT] ## normally, set by parse_opts
$irb = Irb.new(workspace, @CONF[:SCRIPT])
else
$irb = Irb.new(workspace)
end
@CONF[:IRB_RC].call($irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = $irb.context
trap 'INT' do
$irb.signal_handle
end
custom_configuration if defined?(IRB.custom_configuration)
catch :IRB_EXIT do
$irb.eval_input
end
## might want to reset your app's interrupt handler here
end
end
class Object
include IRB::ExtendCommandBundle # so that Marshal.dump works
end
if __FILE__ == $0
x = Object.new
puts "\nStarted irb shell for x"
IRB.start_session(x)
puts "\nStarted irb shell for x with current binding"
IRB.start_session(binding, x)
puts "\nRestarted irb shell for x with current binding"
$irb.eval_input
puts "\nExited irb shell"
p x
end
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407