diff options
Diffstat (limited to 'lib/irb/workspace.rb')
-rw-r--r-- | lib/irb/workspace.rb | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 595e654f07..54e37c9741 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -9,6 +9,9 @@ # # # + +require "delegate" + module IRB # :nodoc: class WorkSpace # Creates a new workspace. @@ -55,11 +58,15 @@ EOF __LINE__ - 3) end end + if main.empty? @main = eval("self", @binding) else @main = main[0] - IRB.conf[:__MAIN__] = @main + end + IRB.conf[:__MAIN__] = @main + + unless main.empty? case @main when Module @binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__) @@ -71,6 +78,28 @@ EOF end end end + + case @main + when Object + use_delegator = @main.frozen? + else + use_delegator = true + end + + if use_delegator + @main = SimpleDelegator.new(@main) + IRB.conf[:__MAIN__] = @main + @main.singleton_class.class_eval do + private + define_method(:exit) do |*a, &b| + # Do nothing, will be overridden + end + define_method(:binding, Kernel.instance_method(:binding)) + define_method(:local_variables, Kernel.instance_method(:local_variables)) + end + @binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location) + end + @binding.local_variable_set(:_, nil) end |