Sha256: 0c6cec4615b3515f4be4d5d5fc3e46254c808d2b4e4f54b367b0553955f3c7f1

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'irb'

$irby_binding_stack = []
set_trace_func lambda { |event, file, line, id, binding, klass|
  case event
  when 'call'
    $irby_binding_stack << binding
  when 'return', 'raise'
    $irby_binding_stack.pop
  end
}

module IRB
  def self.start_session(binding)
    unless @__irb_initialized
      args = ARGV.dup
      ARGV.clear
      IRB.setup(nil)
      ARGV.replace(args)
      @__irb_initialized = true
    end

    workspace = WorkSpace.new(binding)

    irb = Irb.new(workspace)

    @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
    @CONF[:MAIN_CONTEXT] = irb.context

    catch(:IRB_EXIT) do
      irb.eval_input
    end
  end
end

class Object
  def irb(specific_binding = nil)
    if specific_binding
      target_binding = specific_binding
    else
      caller_binding = $irby_binding_stack[-2]
      if self == caller_binding.eval('self')
        target_binding = caller_binding
      else
        target_binding = binding
      end
    end

    IRB.start_session(target_binding)
  end
  alias_method :irby, :irb
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
irby-1.4.0 lib/irby.rb