Class: IRB::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/context.rb,
lib/irb/ext/macruby.rb

Constant Summary collapse

IGNORE_RESULT =
:irb_ignore_result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, explicit_binding = nil) ⇒ Context

Returns a new instance of Context.



16
17
18
19
20
21
22
23
24
# File 'lib/irb/context.rb', line 16

def initialize(object, explicit_binding = nil)
  @object  = object
  @binding = explicit_binding || object.instance_eval { binding }
  @line    = 1
  clear_buffer
  
  @last_result_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
  @exception_assigner   = __evaluate__("e = exception = nil; proc { |ex| e = exception = ex }")
end

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding.



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

def binding
  @binding
end

#formatterObject

Returns the value of attribute formatter.



14
15
16
# File 'lib/irb/context.rb', line 14

def formatter
  @formatter
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#__evaluate__(source, file = __FILE__, line = __LINE__) ⇒ Object



26
27
28
# File 'lib/irb/context.rb', line 26

def __evaluate__(source, file = __FILE__, line = __LINE__)
  eval(source, @binding, file, line)
end

#_runObject



11
# File 'lib/irb/ext/macruby.rb', line 11

alias_method :_run, :run

#clear_bufferObject



83
84
85
# File 'lib/irb/context.rb', line 83

def clear_buffer
  @source = Source.new
end

#evaluate(source) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/irb/context.rb', line 30

def evaluate(source)
  result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
  unless result == IGNORE_RESULT
    store_result(result)
    puts(formatter.result(result))
    result
  end
rescue Exception => e
  store_exception(e)
  puts(formatter.exception(e))
end

#input_line(line) ⇒ Object



74
75
76
77
# File 'lib/irb/context.rb', line 74

def input_line(line)
  puts(formatter.prompt(self) + line)
  process_line(line)
end

#process_line(line) ⇒ Object

Returns whether or not the user wants to continue the current runloop. This can only be done at a code block indentation level of 0.

For instance, this will continue:

process_line("def foo") # => true
process_line("quit") # => true
process_line("end") # => true

But at code block indentation level 0, ‘quit’ means exit the runloop:

process_line("quit") # => false


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/irb/context.rb', line 54

def process_line(line)
  @source << line
  return false if @source.terminate?
  
  if @source.syntax_error?
    puts(formatter.syntax_error(@line, @source.syntax_error))
    @source.pop
  elsif @source.code_block?
    evaluate(@source)
    clear_buffer
  end
  @line += 1
  
  true
end

#promptObject



70
71
72
# File 'lib/irb/context.rb', line 70

def prompt
  formatter.prompt(self)
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/irb/ext/macruby.rb', line 13

def run
  if NSApplication.sharedApplication.running?
    _run
  else
    Thread.new do
      _run
      NSApplication.sharedApplication.terminate(self)
    end
    NSApplication.sharedApplication.run
  end
end

#store_exception(exception) ⇒ Object



91
92
93
# File 'lib/irb/context.rb', line 91

def store_exception(exception)
  @exception_assigner.call(exception)
end

#store_result(result) ⇒ Object



87
88
89
# File 'lib/irb/context.rb', line 87

def store_result(result)
  @last_result_assigner.call(result)
end