Created
April 29, 2011 18:02
-
-
Save Oshuma/948725 to your computer and use it in GitHub Desktop.
Patch to IRB to allow a custom context.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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