Class: IRB::Context
- Inherits:
-
Object
- Object
- IRB::Context
- Defined in:
- lib/irb/context.rb,
lib/irb/ext/macruby.rb
Constant Summary collapse
- IGNORE_RESULT =
:irb_ignore_result
Instance Attribute Summary collapse
-
#binding ⇒ Object
readonly
Returns the value of attribute binding.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #__evaluate__(source, file = __FILE__, line = __LINE__) ⇒ Object
- #_run ⇒ Object
- #clear_buffer ⇒ Object
- #evaluate(source) ⇒ Object
-
#initialize(object, explicit_binding = nil) ⇒ Context
constructor
A new instance of Context.
- #input_line(line) ⇒ Object
-
#process_line(line) ⇒ Object
Returns whether or not the user wants to continue the current runloop.
- #prompt ⇒ Object
- #run ⇒ Object
- #store_exception(exception) ⇒ Object
- #store_result(result) ⇒ Object
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
#binding ⇒ Object (readonly)
Returns the value of attribute binding.
13 14 15 |
# File 'lib/irb/context.rb', line 13 def binding @binding end |
#formatter ⇒ Object
Returns the value of attribute formatter.
14 15 16 |
# File 'lib/irb/context.rb', line 14 def formatter @formatter end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
13 14 15 |
# File 'lib/irb/context.rb', line 13 def line @line end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
13 14 15 |
# File 'lib/irb/context.rb', line 13 def object @object end |
#source ⇒ Object (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 |
#_run ⇒ Object
11 |
# File 'lib/irb/ext/macruby.rb', line 11 alias_method :_run, :run |
#clear_buffer ⇒ Object
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 |
#prompt ⇒ Object
70 71 72 |
# File 'lib/irb/context.rb', line 70 def prompt formatter.prompt(self) end |
#run ⇒ Object
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 |