Class: Sandbox::IRB

Inherits:
Object
  • Object
show all
Defined in:
lib/sandbox/irb.rb

Instance Method Summary collapse

Constructor Details

#initialize(box) ⇒ IRB

Returns a new instance of IRB.



6
7
8
9
10
11
# File 'lib/sandbox/irb.rb', line 6

def initialize(box)
  @box, @sig, @p = box, :IN_IRB
  @box_errors = %w[StandardError ScriptError].map { |x| @box.eval(x) }
  @prompt = {:start => ">> ", :continue => ".. ", :nested => ".. ",
    :string => "   ", :return => "=> %s\n"}
end

Instance Method Details

#box_eval(str) ⇒ Object



13
14
15
# File 'lib/sandbox/irb.rb', line 13

def box_eval(str)
  @box.eval(str)
end

#prompt(prompt, ltype, indent, line_no) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sandbox/irb.rb', line 54

def prompt(prompt, ltype, indent, line_no)
  p = prompt.dup
  p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
    case $2
    # when "N"
    #   @context.irb_name
    # when "m"
    #   @context.main.to_s
    # when "M"
    #   @context.main.inspect
    when "l"
      ltype
    when "i"
      if $1 
        format("%" + $1 + "d", indent)
      else
        indent.to_s
      end
    when "n"
      if $1 
        format("%" + $1 + "d", line_no)
      else
        line_no.to_s
      end
    when "%"
      "%"
    end
  end
  p
end

#signal_status(status) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/sandbox/irb.rb', line 85

def signal_status(status)
  return yield if @sig == :IN_LOAD
  sig_back = @sig
  @sig = status
  begin
    yield
  ensure
    @sig = sig_back
  end
end

#start(io) ⇒ Object



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
# File 'lib/sandbox/irb.rb', line 17

def start(io)
  scanner = RubyLex.new
  scanner.exception_on_syntax_error = false
  scanner.set_prompt do |ltype, indent, continue, line_no|
    if ltype
      f = @prompt[:string]
    elsif continue
      f = @prompt[:continue]
    elsif indent > 0
      f = @prompt[:nested]
    else
      f = @prompt[:start]
    end
    f = "" unless f
    @p = prompt(f, ltype, indent, line_no)
  end

  scanner.set_input(io) do
    signal_status(:IN_INPUT) do
      io.print @p
      io.gets
    end
  end

  scanner.each_top_level_statement do |line, line_no|
    signal_status(:IN_EVAL) do
      line.untaint
      begin
        val = box_eval(line)
        io.puts @prompt[:return] % [val.inspect]
      rescue Sandbox::Exception, Sandbox::TimeoutError => e
        io.print e, "\n"
      end
    end
  end
end