Class: IRB::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/irb/context.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.



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

def initialize(object, explicit_binding = nil)
  @object  = object
  @binding = explicit_binding || object.instance_eval { binding }
  @line    = 1
  @level   = 0
  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.



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

def binding
  @binding
end

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#objectObject (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

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



28
29
30
# File 'lib/irb/context.rb', line 28

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

#clear_bufferObject



114
115
116
# File 'lib/irb/context.rb', line 114

def clear_buffer
  @source = Source.new
end

#driverObject



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

def driver
  IRB::Driver.current
end

#evaluate(source) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/irb/context.rb', line 39

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

#input_line(line) ⇒ Object



105
106
107
108
# File 'lib/irb/context.rb', line 105

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

#output(string) ⇒ Object

Output is directed to the IRB::Driver.current driver’s output if a current driver is available. Otherwise it’s simply printed to $stdout.



93
94
95
96
97
98
99
# File 'lib/irb/context.rb', line 93

def output(string)
  if driver = self.driver
    driver.output.puts(string)
  else
    puts(string)
  end
end

#process_line(line) {|prompt_and_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

If re-indenting the line results in a new line, the reformatted line and prompt are yielded to the optional block. This happens before the line is actually processed, so the caller (driver) has the opportunity to update the last printed line.

Yields:

  • (prompt_and_line)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/irb/context.rb', line 68

def process_line(line)
  prompt_and_line = formatter.reindent_last_line(self) { @source << line }
  yield(*prompt_and_line) if prompt_and_line && block_given?

  return false if @source.terminate?

  if @source.syntax_error?
    output(formatter.syntax_error(@line, @source.syntax_error))
    @source.pop
  elsif @source.code_block?
    evaluate(@source)
    clear_buffer
  end
  @line += 1
  @level = source.level
  
  true
end

#prompt(ignore_auto_indent = false) ⇒ Object



101
102
103
# File 'lib/irb/context.rb', line 101

def prompt(ignore_auto_indent = false)
  formatter.prompt(self, ignore_auto_indent)
end

#store_exception(exception) ⇒ Object



122
123
124
# File 'lib/irb/context.rb', line 122

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

#store_result(result) ⇒ Object



118
119
120
# File 'lib/irb/context.rb', line 118

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

#to_sObject Also known as: inspect



32
33
34
35
36
# File 'lib/irb/context.rb', line 32

def to_s
  object_description = "`#{object.inspect}'"
  object_description = "of class `#{object.class.name}'" if object_description.length > 32
  "#<IRB::Context for object #{object_description}>"
end