Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created April 29, 2011 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oshuma/948725 to your computer and use it in GitHub Desktop.
Save Oshuma/948725 to your computer and use it in GitHub Desktop.
Patch to IRB to allow a custom context.
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
end
workspace = WorkSpace.new(*args)
if @CONF[:SCRIPT]
$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
end
end
class Object
include IRB::ExtendCommandBundle # so that Marshal.dump works
end
# Instance context
class Foo
def do_stuff
local_var = 'this is local'
# Run IRB in the context of self.
IRB.start_session(self)
end
end
# Binding to an object
foo = 'some fooking string'
# Context will be the string instance in 'foo'
IRB.start_session(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment