Class: IRB::Context

Inherits:
Object show all
Defined in:
lib/irb/context.rb,
lib/irb/ext/tracer.rb,
lib/irb/ext/history.rb,
lib/irb/ext/change-ws.rb,
lib/irb/ext/math-mode.rb,
lib/irb/ext/use-loader.rb,
lib/irb/ext/workspaces.rb,
lib/irb/ext/save-history.rb

Constant Summary collapse

NOPRINTING_IVARS =
["@last_value"]
NO_INSPECTING_IVARS =
["@irb", "@io"]
IDNAME_IVARS =
["@prompt_mode"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(irb, workspace = nil, input_method = nil, output_method = nil) ⇒ Context

Arguments:

input_method: nil -- stdin or readline

String -- File other -- using this as InputMethod



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/irb/context.rb', line 22

def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
  @irb = irb
  if workspace
	@workspace = workspace
  else
	@workspace = WorkSpace.new
  end
  @thread = Thread.current if defined? Thread
#      @irb_level = 0

  # copy of default configuration
  @ap_name = IRB.conf[:AP_NAME]
  @rc = IRB.conf[:RC]
  @load_modules = IRB.conf[:LOAD_MODULES]

  @use_readline = IRB.conf[:USE_READLINE]
  @inspect_mode = IRB.conf[:INSPECT_MODE]

  self.math_mode = IRB.conf[:MATH_MODE] if IRB.conf[:MATH_MODE]
  self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
  self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
  self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]

  @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
  @ignore_eof = IRB.conf[:IGNORE_EOF]

  @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
  
  self.prompt_mode = IRB.conf[:PROMPT_MODE]

  if IRB.conf[:SINGLE_IRB] or !defined?(JobManager)
	@irb_name = IRB.conf[:IRB_NAME]
  else
	@irb_name = "irb#"+IRB.JobManager.n_jobs.to_s
  end
  @irb_path = "(" + @irb_name + ")"

  case input_method
  when nil
	case use_readline?
	when nil
	  if (defined?(ReadlineInputMethod) && STDIN.tty? &&
   IRB.conf[:PROMPT_MODE] != :INF_RUBY)
 @io = ReadlineInputMethod.new
	  else
 @io = StdioInputMethod.new
	  end
	when false
	  @io = StdioInputMethod.new
	when true
	  if defined?(ReadlineInputMethod)
 @io = ReadlineInputMethod.new
	  else
 @io = StdioInputMethod.new
	  end
	end

  when String
	@io = FileInputMethod.new(input_method)
	@irb_name = File.basename(input_method)
	@irb_path = input_method
  else
	@io = input_method
  end
  self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]

  if output_method
	@output_method = output_method
  else
	@output_method = StdioOutputMethod.new
  end

  @verbose = IRB.conf[:VERBOSE] 
  @echo = IRB.conf[:ECHO]
  if @echo.nil?
	@echo = true
  end
  @debug_level = IRB.conf[:DEBUG_LEVEL]
end

Instance Attribute Details

#ap_nameObject

Returns the value of attribute ap_name



112
113
114
# File 'lib/irb/context.rb', line 112

def ap_name
  @ap_name
end

#auto_indent_modeObject

Returns the value of attribute auto_indent_mode



126
127
128
# File 'lib/irb/context.rb', line 126

def auto_indent_mode
  @auto_indent_mode
end

#back_trace_limitObject

Returns the value of attribute back_trace_limit



135
136
137
# File 'lib/irb/context.rb', line 135

def back_trace_limit
  @back_trace_limit
end

#debug_levelObject

Returns the value of attribute debug_level



133
134
135
# File 'lib/irb/context.rb', line 133

def debug_level
  @debug_level
end

#echoObject Also known as: echo?

Returns the value of attribute echo



131
132
133
# File 'lib/irb/context.rb', line 131

def echo
  @echo
end

#eval_historyObject

Returns the value of attribute eval_history



33
34
35
# File 'lib/irb/ext/history.rb', line 33

def eval_history
  @eval_history
end

#ignore_eofObject Also known as: ignore_eof?

Returns the value of attribute ignore_eof



130
131
132
# File 'lib/irb/context.rb', line 130

def ignore_eof
  @ignore_eof
end

#ignore_sigintObject Also known as: ignore_sigint?

Returns the value of attribute ignore_sigint



129
130
131
# File 'lib/irb/context.rb', line 129

def ignore_sigint
  @ignore_sigint
end

#inspect_modeObject

Returns the value of attribute inspect_mode



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

def inspect_mode
  @inspect_mode
end

#ioObject

Returns the value of attribute io



109
110
111
# File 'lib/irb/context.rb', line 109

def io
  @io
end

#irbObject

Returns the value of attribute irb



111
112
113
# File 'lib/irb/context.rb', line 111

def irb
  @irb
end

#irb_nameObject

Returns the value of attribute irb_name



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

def irb_name
  @irb_name
end

#irb_pathObject

Returns the value of attribute irb_path



116
117
118
# File 'lib/irb/context.rb', line 116

def irb_path
  @irb_path
end

#last_valueObject (readonly)

Returns the value of attribute last_value



160
161
162
# File 'lib/irb/context.rb', line 160

def last_value
  @last_value
end

#load_modulesObject

Returns the value of attribute load_modules



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

def load_modules
  @load_modules
end

#math_modeObject Also known as: math?

Returns the value of attribute math_mode



16
17
18
# File 'lib/irb/ext/math-mode.rb', line 16

def math_mode
  @math_mode
end

#prompt_cObject

Returns the value of attribute prompt_c



124
125
126
# File 'lib/irb/context.rb', line 124

def prompt_c
  @prompt_c
end

#prompt_iObject

Returns the value of attribute prompt_i



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

def prompt_i
  @prompt_i
end

#prompt_modeObject

Returns the value of attribute prompt_mode



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

def prompt_mode
  @prompt_mode
end

#prompt_nObject

Returns the value of attribute prompt_n



125
126
127
# File 'lib/irb/context.rb', line 125

def prompt_n
  @prompt_n
end

#prompt_sObject

Returns the value of attribute prompt_s



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

def prompt_s
  @prompt_s
end

#rcObject Also known as: rc?

Returns the value of attribute rc



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

def rc
  @rc
end

#return_formatObject

Returns the value of attribute return_format



127
128
129
# File 'lib/irb/context.rb', line 127

def return_format
  @return_format
end

#threadObject (readonly)

Returns the value of attribute thread



108
109
110
# File 'lib/irb/context.rb', line 108

def thread
  @thread
end

#use_readlineObject Also known as: use_readline?

Returns the value of attribute use_readline



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

def use_readline
  @use_readline
end

#use_tracerObject Also known as: use_tracer?

Returns the value of attribute use_tracer



27
28
29
# File 'lib/irb/ext/tracer.rb', line 27

def use_tracer
  @use_tracer
end

#verboseObject

Returns the value of attribute verbose



132
133
134
# File 'lib/irb/context.rb', line 132

def verbose
  @verbose
end

#workspaceObject

Returns the value of attribute workspace



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

def workspace
  @workspace
end

#workspace_homeObject (readonly)

Returns the value of attribute workspace_home



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

def workspace_home
  @workspace_home
end

Instance Method Details

#change_workspace(*_main) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/irb/ext/change-ws.rb', line 24

def change_workspace(*_main)
  if _main.empty?
	@workspace = home_workspace 
	return main
  end
  
  @workspace = WorkSpace.new(_main[0])
  
  if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
	main.extend ExtendCommandBundle
  end
end

#debug?Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/irb/context.rb', line 213

def debug?
  @debug_level > 0
end

#evaluate(line, line_no) ⇒ Object



217
218
219
220
221
222
# File 'lib/irb/context.rb', line 217

def evaluate(line, line_no)
  @line_no = line_no
  set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
#      @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
#      @_ = @workspace.evaluate(line, irb_path, line_no)
end

#exit(ret = 0) ⇒ Object Also known as: __exit__



225
226
227
# File 'lib/irb/context.rb', line 225

def exit(ret = 0)
  IRB.irb_exit(@irb, ret)
end

#file_input?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/irb/context.rb', line 188

def file_input?
  @io.class == FileInputMethod
end

#history_fileObject



41
42
43
# File 'lib/irb/ext/save-history.rb', line 41

def history_file
  IRB.conf[:HISTORY_FILE]
end

#history_file=(hist) ⇒ Object



45
46
47
# File 'lib/irb/ext/save-history.rb', line 45

def history_file=(hist)
  IRB.conf[:HISTORY_FILE] = hist
end

#home_workspaceObject



16
17
18
19
20
21
22
# File 'lib/irb/ext/change-ws.rb', line 16

def home_workspace
  if defined? @home_workspace
	@home_workspace
  else
	@home_workspace = @workspace
  end
end

#init_save_historyObject



22
23
24
25
26
# File 'lib/irb/ext/save-history.rb', line 22

def init_save_history
  unless (class<<@io;self;end).include?(HistorySavingAbility)
	@io.extend(HistorySavingAbility)
  end
end

#inspectObject Also known as: __inspect__, to_s



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/irb/context.rb', line 234

def inspect
  array = []
  for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
	name = ivar.sub(/^@(.*)$/){$1}
	val = instance_eval(ivar)
	case ivar
	when *NOPRINTING_IVARS
	  array.push format("conf.%s=%s", name, "...")
	when *NO_INSPECTING_IVARS
	  array.push format("conf.%s=%s", name, val.to_s)
	when *IDNAME_IVARS
	  array.push format("conf.%s=:%s", name, val.id2name)
	else
	  array.push format("conf.%s=%s", name, val.inspect)
	end
  end
  array.join("\n")
end

#inspect?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/irb/context.rb', line 184

def inspect?
  @inspect_mode.nil? && !@math_mode or @inspect_mode
end

#irb_levelObject



16
17
18
# File 'lib/irb/ext/workspaces.rb', line 16

def irb_level
  workspace_stack.size
end

#mainObject



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

def main
  @workspace.main
end

#pop_workspaceObject



47
48
49
50
51
52
53
# File 'lib/irb/ext/workspaces.rb', line 47

def pop_workspace
  if workspaces.empty?
	print "workspace stack empty\n"
	return
  end
  @workspace = workspaces.pop
end

#prompting?Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/irb/context.rb', line 155

def prompting?
  verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
		(defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
end

#push_workspace(*_main) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/irb/ext/workspaces.rb', line 28

def push_workspace(*_main)
  if _main.empty?
	if workspaces.empty?
	  print "No other workspace\n"
	  return nil
	end
	ws = workspaces.pop
	workspaces.push @workspace
	@workspace = ws
	return workspaces
  end

  workspaces.push @workspace
  @workspace = WorkSpace.new(@workspace.binding, _main[0])
  if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
	main.extend ExtendCommandBundle
  end
end

#save_historyObject



28
29
30
# File 'lib/irb/ext/save-history.rb', line 28

def save_history
  IRB.conf[:SAVE_HISTORY]
end

#save_history=(val) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/irb/ext/save-history.rb', line 32

def save_history=(val)
  IRB.conf[:SAVE_HISTORY] = val
  if val
	main_context = IRB.conf[:MAIN_CONTEXT]
	main_context = self unless main_context
	main_context.init_save_history
  end
end

#set_last_value(value) ⇒ Object Also known as: _set_last_value



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/irb/context.rb', line 162

def set_last_value(value)
  _set_last_value(value)

#      @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
  if @eval_history #and !@eval_history_values.equal?(llv)
 	@eval_history_values.push @line_no, @last_value
 	@workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
  end

  @last_value
end

#use_loaderObject Also known as: use_loader?



35
36
37
# File 'lib/irb/ext/use-loader.rb', line 35

def use_loader
  IRB.conf[:USE_LOADER]
end

#use_loader=(opt) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/irb/ext/use-loader.rb', line 41

def use_loader=(opt)

  if IRB.conf[:USE_LOADER] != opt
	IRB.conf[:USE_LOADER] = opt
	if opt
	  if !$".include?("irb/cmd/load")
	  end
	  (class<<@workspace.main;self;end).instance_eval {
 alias_method :load, :irb_load
 alias_method :require, :irb_require
	  }
	else
	  (class<<@workspace.main;self;end).instance_eval {
 alias_method :load, :__original__load__IRB_use_loader__
 alias_method :require, :__original__require__IRB_use_loader__
	  }
	end
  end
  print "Switch to load/require#{unless use_loader; ' non';end} trace mode.\n" if verbose?
  opt
end

#verbose?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
# File 'lib/irb/context.rb', line 143

def verbose?
  if @verbose.nil?
	if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod) 
	  false
	elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
	  true
	else
	  false
	end
  end
end

#workspacesObject



20
21
22
23
24
25
26
# File 'lib/irb/ext/workspaces.rb', line 20

def workspaces
  if defined? @workspaces
	@workspaces
  else
	@workspaces = []
  end
end