1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'irb'

# mercilessly borrowed from: http://errtheblog.com/posts/9-drop-to-irb
#
# I've cleaned up a few things and moved some stuff around, not tested at all of course.
#
# Usage:
#
# raggi@mbk:~/dev/gosu$ ruby -rraggi/irb/drop -e "puts 'hi'; dROP! []; puts 'bye'"hi
# Helper Methods: continue, instance, meths, quit!
# Variables: object # => [], binding # => #<Binding:0x39c9c>
# >> self.concat [:foo,:bar,:baz]
# => [:foo, :bar, :baz]
# >> reverse!
# => [:baz, :bar, :foo]
# >> continue
# bye

module IRB
  class <<self
    attr_accessor :instance
  end
  
  def self.start_session(binding)
    old_args = ARGV
    ARGV.size.times { ARGV.shift }
    
    IRB.setup(nil)

    workspace = WorkSpace.new(binding)

    if @CONF[:SCRIPT]
      self.instance = Irb.new(workspace, @CONF[:SCRIPT])
    else
      self.instance = Irb.new(workspace)
    end

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

    catch(:IRB_EXIT) do
      self.instance.eval_input
    end
  ensure
    old_args.each { |a| ARGV << a }
  end
end

module IRBHelper
  def meths(o); puts (o.methods.sort - Class.new.methods).join("\n"); end
  def quit!; irb_exit; ::Process.exit!; end
  def instance; IRB.instance; end
  def continue; irb_exit; end
end

def dROP! object = nil
  binding = case object
  when Binding
    object
  when NilClass
    Kernel.binding()
  else
    object.send(:binding)
  end

  if defined? IRBHelper
    puts "Helper Methods: #{(Class.new.instance_eval {include IRBHelper;self}.new.methods.sort - Class.new.methods).join(', ')}"
    puts "Variables: #{local_variables.map{|v| "#{v} # => #{eval(v).inspect}"}.join(', ')}"
    include IRBHelper
  end
  
  IRB.start_session binding
end