From: Joel VanderWerf Date: 2004-05-05T02:14:14+09:00 Subject: Re: Can a program be made to drop into irb? This is what I use to break from a program into an irb shell. I set my app's interrupt handler to call it, and then reset the interrupt handler when it is done (where noted). You can go back and forth between app and shell with ^C and ^D. I usually add some methods to the object that gets passed to start_session: #help, #commands, methods to pipe data to gnuplot, etc. Ara's idea to pass a binding to Workspace.new is great. Changes to locals are even persistent after you leave the shell. I'm puzzled about why that works--maybe irb detects that the object is a binding? -- #!/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(object) unless $irb IRB.setup nil ## maybe set some opts here, as in parse_opts in irb/init.rb? end workspace = WorkSpace.new(object) 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 current binding" IRB.start_session(binding) puts "\nExited irb shell" p x end