Sha256: f6c1dbd357254cb1b096dc68b19ff9a0a8f09c45c565851d6ea8a12520416dae

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'irb'

module IRBy
  def self.binding_stack
    @binding_stack ||= []
  end
end

set_trace_func lambda { |event, file, line, id, binding, klass|
  case event
  when /call$/
    IRBy.binding_stack << binding
  when /return$/
    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.1 lib/irby.rb