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/workspaces.rb,
lib/irb/ext/use-loader.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
101
# 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]
  @verbose = IRB.conf[:VERBOSE]
  @io = nil

  self.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

  @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



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

def ap_name
  @ap_name
end

#auto_indent_modeObject

Returns the value of attribute auto_indent_mode



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

def auto_indent_mode
  @auto_indent_mode
end

#back_trace_limitObject

Returns the value of attribute back_trace_limit



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

def back_trace_limit
  @back_trace_limit
end

#debug_levelObject

Returns the value of attribute debug_level



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

def debug_level
  @debug_level
end

#echoObject Also known as: echo?

Returns the value of attribute echo



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

def echo
  @echo
end

#eval_historyObject

Returns the value of attribute eval_history



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

def eval_history
  @eval_history
end

#ignore_eofObject Also known as: ignore_eof?

Returns the value of attribute ignore_eof



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

def ignore_eof
  @ignore_eof
end

#ignore_sigintObject Also known as: ignore_sigint?

Returns the value of attribute ignore_sigint



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

def ignore_sigint
  @ignore_sigint
end

#inspect_modeObject

Returns the value of attribute inspect_mode



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

def inspect_mode
  @inspect_mode
end

#ioObject

Returns the value of attribute io



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

def io
  @io
end

#irbObject

Returns the value of attribute irb



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

def irb
  @irb
end

#irb_nameObject

Returns the value of attribute irb_name



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

def irb_name
  @irb_name
end

#irb_pathObject

Returns the value of attribute irb_path



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

def irb_path
  @irb_path
end

#last_valueObject (readonly)

Returns the value of attribute last_value



163
164
165
# File 'lib/irb/context.rb', line 163

def last_value
  @last_value
end

#load_modulesObject

Returns the value of attribute load_modules



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

def load_modules
  @load_modules
end

#math_modeObject Also known as: math?

Returns the value of attribute math_mode



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

def math_mode
  @math_mode
end

#prompt_cObject

Returns the value of attribute prompt_c



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

def prompt_c
  @prompt_c
end

#prompt_iObject

Returns the value of attribute prompt_i



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

def prompt_i
  @prompt_i
end

#prompt_modeObject

Returns the value of attribute prompt_mode



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

def prompt_mode
  @prompt_mode
end

#prompt_nObject

Returns the value of attribute prompt_n



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

def prompt_n
  @prompt_n
end

#prompt_sObject

Returns the value of attribute prompt_s



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

def prompt_s
  @prompt_s
end

#rcObject Also known as: rc?

Returns the value of attribute rc



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

def rc
  @rc
end

#return_formatObject

Returns the value of attribute return_format



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

def return_format
  @return_format
end

#threadObject (readonly)

Returns the value of attribute thread



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

def thread
  @thread
end

#use_readlineObject Also known as: use_readline?

Returns the value of attribute use_readline



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

def use_readline
  @use_readline
end

#use_tracerObject Also known as: use_tracer?

Returns the value of attribute use_tracer



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

def use_tracer
  @use_tracer
end

#verboseObject

Returns the value of attribute verbose



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

def verbose
  @verbose
end

#workspaceObject

Returns the value of attribute workspace



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

def workspace
  @workspace
end

#workspace_homeObject (readonly)

Returns the value of attribute workspace_home



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

def workspace_home
  @workspace_home
end

Instance Method Details

#__exit__Object



263
# File 'lib/irb/context.rb', line 263

alias __exit__ exit

#__inspect__Object



272
# File 'lib/irb/context.rb', line 272

alias __inspect__ inspect

#_set_last_valueObject



18
19
20
21
# File 'lib/irb/ext/history.rb', line 18

def set_last_value(value)
  @last_value = value
  @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
end

#change_workspace(*_main) ⇒ Object



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

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)


248
249
250
# File 'lib/irb/context.rb', line 248

def debug?
  @debug_level > 0
end

#evaluate(line, line_no) ⇒ Object



252
253
254
255
256
257
# File 'lib/irb/context.rb', line 252

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



264
265
266
# File 'lib/irb/context.rb', line 264

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

#file_input?Boolean

Returns:

  • (Boolean)


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

def file_input?
  @io.class == FileInputMethod
end

#history_fileObject



38
39
40
# File 'lib/irb/ext/save-history.rb', line 38

def history_file
  IRB.conf[:HISTORY_FILE]
end

#history_file=(hist) ⇒ Object



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

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

#home_workspaceObject



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

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

#init_save_historyObject



19
20
21
22
23
# File 'lib/irb/ext/save-history.rb', line 19

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

#inspectObject Also known as: to_s



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/irb/context.rb', line 273

def inspect
  array = []
  for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
	ivar = ivar.to_s
	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)


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

def inspect?
  @inspect_mode.nil? or @inspect_mode
end

#inspect_last_valueObject



259
260
261
# File 'lib/irb/context.rb', line 259

def inspect_last_value
  @inspect_method.inspect_value(@last_value)
end

#irb_levelObject



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

def irb_level
  workspace_stack.size
end

#mainObject



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

def main
  @workspace.main
end

#pop_workspaceObject



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

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

#prompting?Boolean

Returns:

  • (Boolean)


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

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

#push_workspace(*_main) ⇒ Object



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

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



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

def save_history
  IRB.conf[:SAVE_HISTORY]
end

#save_history=(val) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/irb/ext/save-history.rb', line 29

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



165
166
167
168
# File 'lib/irb/context.rb', line 165

def set_last_value(value)
  @last_value = value
  @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
end

#use_loaderObject Also known as: use_loader?



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

def use_loader
  IRB.conf[:USE_LOADER]
end

#use_loader=(opt) ⇒ Object



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

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)


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

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
  else
	@verbose
  end
end

#workspacesObject



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

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