Class: IRB::Context
- 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
Overview
A class that wraps the current state of the irb session, including the configuration of IRB.conf.
Constant Summary collapse
- NOPRINTING_IVARS =
:nodoc:
["@last_value"]
- NO_INSPECTING_IVARS =
:nodoc:
["@irb", "@io"]
- IDNAME_IVARS =
:nodoc:
["@prompt_mode"]
Instance Attribute Summary collapse
-
#ap_name ⇒ Object
A copy of the default
IRB.conf[:AP_NAME]
. -
#auto_indent_mode ⇒ Object
Can be either the default
IRB.conf[:AUTO_INDENT]
, or the mode set by #prompt_mode=. -
#back_trace_limit ⇒ Object
The limit of backtrace lines displayed as top
n
and tailn
. -
#debug_level ⇒ Object
The debug level of irb.
-
#echo ⇒ Object
(also: #echo?)
Whether to echo the return value to output or not.
-
#eval_history ⇒ Object
The command result history limit.
-
#ignore_eof ⇒ Object
(also: #ignore_eof?)
Whether
^D
(control-d
) will be ignored or not. -
#ignore_sigint ⇒ Object
(also: #ignore_sigint?)
Whether
^C
(control-c
) will be ignored or not. -
#inspect_mode ⇒ Object
A copy of the default
IRB.conf[:INSPECT_MODE]
. -
#io ⇒ Object
The current input method.
-
#irb ⇒ Object
Current irb session.
-
#irb_name ⇒ Object
Can be either name from
IRB.conf[:IRB_NAME]
, or the number of the current job set by JobManager, such asirb#2
. -
#irb_path ⇒ Object
Can be either the #irb_name surrounded by parenthesis, or the
input_method
passed to Context.new. -
#last_value ⇒ Object
readonly
The return value of the last statement evaluated.
-
#load_modules ⇒ Object
A copy of the default
IRB.conf[:LOAD_MODULES]
. -
#math_mode ⇒ Object
(also: #math?)
Returns whether bc mode is enabled.
-
#prompt_c ⇒ Object
IRB prompt for continuated statement (e.g. immediately after an
if
). -
#prompt_i ⇒ Object
Standard IRB prompt.
-
#prompt_mode ⇒ Object
A copy of the default
IRB.conf[:PROMPT_MODE]
. -
#prompt_n ⇒ Object
See IRB@Customizing+the+IRB+Prompt for more information.
-
#prompt_s ⇒ Object
IRB prompt for continuated strings.
-
#rc ⇒ Object
(also: #rc?)
A copy of the default
IRB.conf[:RC]
. -
#return_format ⇒ Object
The format of the return statement, set by #prompt_mode= using the
:RETURN
of themode
passed to set the current #prompt_mode. -
#thread ⇒ Object
readonly
The current thread in this context.
-
#use_readline ⇒ Object
(also: #use_readline?)
Whether
Readline
is enabled or not. -
#use_tracer ⇒ Object
(also: #use_tracer?)
Whether Tracer is used when evaluating statements in this context.
-
#verbose ⇒ Object
Whether verbose messages are displayed or not.
-
#workspace ⇒ Object
WorkSpace in the current context.
-
#workspace_home ⇒ Object
readonly
The toplevel workspace, see #home_workspace.
Instance Method Summary collapse
- #__exit__ ⇒ Object
- #__inspect__ ⇒ Object
-
#_set_last_value ⇒ Object
Sets the return value from the last statement evaluated in this context to #last_value.
-
#change_workspace(*_main) ⇒ Object
Changes the current workspace to given object or binding.
-
#debug? ⇒ Boolean
Whether or not debug mode is enabled, see #debug_level=.
-
#evaluate(line, line_no) ⇒ Object
:nodoc:.
-
#exit(ret = 0) ⇒ Object
Exits the current session, see IRB.irb_exit.
-
#file_input? ⇒ Boolean
Whether #io uses a File for the
input_method
passed when creating the current context, see ::new. -
#history_file ⇒ Object
A copy of the default
IRB.conf[:HISTORY_FILE]
. -
#history_file=(hist) ⇒ Object
Set
IRB.conf[:HISTORY_FILE]
to the givenhist
. -
#home_workspace ⇒ Object
Inherited from
TOPLEVEL_BINDING
. -
#init_save_history ⇒ Object
:nodoc:.
-
#initialize(irb, workspace = nil, input_method = nil, output_method = nil) ⇒ Context
constructor
Creates a new IRB context.
-
#inspect ⇒ Object
(also: #to_s)
:nodoc:.
-
#inspect? ⇒ Boolean
Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
-
#inspect_last_value ⇒ Object
:nodoc:.
-
#irb_level ⇒ Object
Size of the current WorkSpace stack.
-
#main ⇒ Object
The top-level workspace, see WorkSpace#main.
-
#pop_workspace ⇒ Object
Removes the last element from the current #workspaces stack and returns it, or
nil
if the current workspace stack is empty. -
#prompting? ⇒ Boolean
Whether #verbose? is
true
, andinput_method
is either StdioInputMethod or ReadlineInputMethod, see #io for more information. -
#push_workspace(*_main) ⇒ Object
Creates a new workspace with the given object or binding, and appends it onto the current #workspaces stack.
-
#save_history ⇒ Object
A copy of the default
IRB.conf[:SAVE_HISTORY]
. -
#save_history=(val) ⇒ Object
Sets
IRB.conf[:SAVE_HISTORY]
to the givenval
and calls #init_save_history with this context. -
#set_last_value(value) ⇒ Object
Sets the return value from the last statement evaluated in this context to #last_value.
-
#use_loader ⇒ Object
(also: #use_loader?)
Returns whether
irb
‘s own file reader method is used byload
/require
or not. -
#use_loader=(opt) ⇒ Object
Sets IRB.conf.
-
#verbose? ⇒ Boolean
Returns whether messages are displayed or not.
-
#workspaces ⇒ Object
WorkSpaces in the current stack.
Constructor Details
#initialize(irb, workspace = nil, input_method = nil, output_method = nil) ⇒ Context
Creates a new IRB context.
The optional input_method
argument:
nil
-
uses stdin or Readline
String
-
uses a File
other
-
uses this as InputMethod
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 102 103 104 |
# File 'lib/irb/context.rb', line 25 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?(IRB::JobManager) @irb_name = IRB.conf[:IRB_NAME] else @irb_name = IRB.conf[:IRB_NAME]+"#"+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 self.debug_level = IRB.conf[:DEBUG_LEVEL] end |
Instance Attribute Details
#ap_name ⇒ Object
A copy of the default IRB.conf[:AP_NAME]
127 128 129 |
# File 'lib/irb/context.rb', line 127 def ap_name @ap_name end |
#auto_indent_mode ⇒ Object
180 181 182 |
# File 'lib/irb/context.rb', line 180 def auto_indent_mode @auto_indent_mode end |
#back_trace_limit ⇒ Object
The limit of backtrace lines displayed as top n
and tail n
.
The default value is 16.
Can also be set using the --back-trace-limit
command line option.
See IRB@Command+line+options for more command line options.
225 226 227 |
# File 'lib/irb/context.rb', line 225 def back_trace_limit @back_trace_limit end |
#debug_level ⇒ Object
The debug level of irb
See #debug_level= for more information.
216 217 218 |
# File 'lib/irb/context.rb', line 216 def debug_level @debug_level end |
#echo ⇒ Object Also known as: echo?
208 209 210 |
# File 'lib/irb/context.rb', line 208 def echo @echo end |
#eval_history ⇒ Object
The command result history limit.
34 35 36 |
# File 'lib/irb/ext/history.rb', line 34 def eval_history @eval_history end |
#ignore_eof ⇒ Object Also known as: ignore_eof?
Whether ^D
(control-d
) will be ignored or not.
If set to false
, ^D
will quit irb.
197 198 199 |
# File 'lib/irb/context.rb', line 197 def ignore_eof @ignore_eof end |
#ignore_sigint ⇒ Object Also known as: ignore_sigint?
Whether ^C
(control-c
) will be ignored or not.
If set to false
, ^C
will quit irb.
If set to true
,
-
during input: cancel input then return to top level.
-
during execute: abandon current execution.
193 194 195 |
# File 'lib/irb/context.rb', line 193 def ignore_sigint @ignore_sigint end |
#inspect_mode ⇒ Object
A copy of the default IRB.conf[:INSPECT_MODE]
146 147 148 |
# File 'lib/irb/context.rb', line 146 def inspect_mode @inspect_mode end |
#io ⇒ Object
The current input method
Can be either StdioInputMethod, ReadlineInputMethod, FileInputMethod or other specified when the context is created. See ::new for more information on input_method
.
122 123 124 |
# File 'lib/irb/context.rb', line 122 def io @io end |
#irb ⇒ Object
Current irb session
125 126 127 |
# File 'lib/irb/context.rb', line 125 def irb @irb end |
#irb_name ⇒ Object
Can be either name from IRB.conf[:IRB_NAME]
, or the number of the current job set by JobManager, such as irb#2
134 135 136 |
# File 'lib/irb/context.rb', line 134 def irb_name @irb_name end |
#irb_path ⇒ Object
Can be either the #irb_name surrounded by parenthesis, or the input_method
passed to Context.new
137 138 139 |
# File 'lib/irb/context.rb', line 137 def irb_path @irb_path end |
#last_value ⇒ Object (readonly)
The return value of the last statement evaluated.
258 259 260 |
# File 'lib/irb/context.rb', line 258 def last_value @last_value end |
#load_modules ⇒ Object
A copy of the default IRB.conf[:LOAD_MODULES]
131 132 133 |
# File 'lib/irb/context.rb', line 131 def load_modules @load_modules end |
#math_mode ⇒ Object Also known as: math?
Returns whether bc mode is enabled.
See #math_mode=
18 19 20 |
# File 'lib/irb/ext/math-mode.rb', line 18 def math_mode @math_mode end |
#prompt_c ⇒ Object
IRB prompt for continuated statement (e.g. immediately after an if
)
See IRB@Customizing+the+IRB+Prompt for more information.
161 162 163 |
# File 'lib/irb/context.rb', line 161 def prompt_c @prompt_c end |
#prompt_i ⇒ Object
Standard IRB prompt
See IRB@Customizing+the+IRB+Prompt for more information.
153 154 155 |
# File 'lib/irb/context.rb', line 153 def prompt_i @prompt_i end |
#prompt_mode ⇒ Object
A copy of the default IRB.conf[:PROMPT_MODE]
149 150 151 |
# File 'lib/irb/context.rb', line 149 def prompt_mode @prompt_mode end |
#prompt_n ⇒ Object
See IRB@Customizing+the+IRB+Prompt for more information.
163 164 165 |
# File 'lib/irb/context.rb', line 163 def prompt_n @prompt_n end |
#prompt_s ⇒ Object
IRB prompt for continuated strings
See IRB@Customizing+the+IRB+Prompt for more information.
157 158 159 |
# File 'lib/irb/context.rb', line 157 def prompt_s @prompt_s end |
#rc ⇒ Object Also known as: rc?
A copy of the default IRB.conf[:RC]
129 130 131 |
# File 'lib/irb/context.rb', line 129 def rc @rc end |
#return_format ⇒ Object
The format of the return statement, set by #prompt_mode= using the :RETURN
of the mode
passed to set the current #prompt_mode.
183 184 185 |
# File 'lib/irb/context.rb', line 183 def return_format @return_format end |
#thread ⇒ Object (readonly)
The current thread in this context
116 117 118 |
# File 'lib/irb/context.rb', line 116 def thread @thread end |
#use_readline ⇒ Object Also known as: use_readline?
Whether Readline
is enabled or not.
A copy of the default IRB.conf[:USE_READLINE]
See #use_readline= for more information.
144 145 146 |
# File 'lib/irb/context.rb', line 144 def use_readline @use_readline end |
#use_tracer ⇒ Object Also known as: use_tracer?
Whether Tracer is used when evaluating statements in this context.
See lib/tracer.rb
for more information.
29 30 31 |
# File 'lib/irb/ext/tracer.rb', line 29 def use_tracer @use_tracer end |
#verbose ⇒ Object
Whether verbose messages are displayed or not.
A copy of the default IRB.conf[:VERBOSE]
212 213 214 |
# File 'lib/irb/context.rb', line 212 def verbose @verbose end |
#workspace ⇒ Object
WorkSpace in the current context
114 115 116 |
# File 'lib/irb/context.rb', line 114 def workspace @workspace end |
#workspace_home ⇒ Object (readonly)
The toplevel workspace, see #home_workspace
112 113 114 |
# File 'lib/irb/context.rb', line 112 def workspace_home @workspace_home end |
Instance Method Details
#__exit__ ⇒ Object
389 |
# File 'lib/irb/context.rb', line 389 alias __exit__ exit |
#__inspect__ ⇒ Object
399 |
# File 'lib/irb/context.rb', line 399 alias __inspect__ inspect |
#_set_last_value ⇒ Object
Sets the return value from the last statement evaluated in this context to #last_value. See #set_last_value
19 20 21 22 |
# File 'lib/irb/ext/history.rb', line 19 def set_last_value(value) @last_value = value @workspace.evaluate self, "_ = IRB.CurrentContext.last_value" end |
#change_workspace(*_main) ⇒ Object
Changes the current workspace to given object or binding.
If the optional argument is omitted, the workspace will be #home_workspace which is inherited from TOPLEVEL_BINDING
or the main object, IRB.conf[:MAIN_CONTEXT]
when irb was initialized.
See IRB::WorkSpace.new for more information.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/irb/ext/change-ws.rb', line 31 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
Whether or not debug mode is enabled, see #debug_level=.
374 375 376 |
# File 'lib/irb/context.rb', line 374 def debug? @debug_level > 0 end |
#evaluate(line, line_no) ⇒ Object
:nodoc:
378 379 380 381 382 383 |
# File 'lib/irb/context.rb', line 378 def evaluate(line, line_no) # :nodoc: @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
Exits the current session, see IRB.irb_exit
391 392 393 |
# File 'lib/irb/context.rb', line 391 def exit(ret = 0) IRB.irb_exit(@irb, ret) end |
#file_input? ⇒ Boolean
Whether #io uses a File for the input_method
passed when creating the current context, see ::new
292 293 294 |
# File 'lib/irb/context.rb', line 292 def file_input? @io.class == FileInputMethod end |
#history_file ⇒ Object
A copy of the default IRB.conf[:HISTORY_FILE]
49 50 51 |
# File 'lib/irb/ext/save-history.rb', line 49 def history_file IRB.conf[:HISTORY_FILE] end |
#history_file=(hist) ⇒ Object
Set IRB.conf[:HISTORY_FILE]
to the given hist
.
54 55 56 |
# File 'lib/irb/ext/save-history.rb', line 54 def history_file=(hist) IRB.conf[:HISTORY_FILE] = hist end |
#home_workspace ⇒ Object
Inherited from TOPLEVEL_BINDING
.
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_history ⇒ Object
:nodoc:
19 20 21 22 23 |
# File 'lib/irb/ext/save-history.rb', line 19 def init_save_history# :nodoc: unless (class<<@io;self;end).include?(HistorySavingAbility) @io.extend(HistorySavingAbility) end end |
#inspect ⇒ Object Also known as: to_s
:nodoc:
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/irb/context.rb', line 400 def inspect # :nodoc: 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
Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
286 287 288 |
# File 'lib/irb/context.rb', line 286 def inspect? @inspect_mode.nil? or @inspect_mode end |
#inspect_last_value ⇒ Object
:nodoc:
385 386 387 |
# File 'lib/irb/context.rb', line 385 def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end |
#irb_level ⇒ Object
Size of the current WorkSpace stack
16 17 18 |
# File 'lib/irb/ext/workspaces.rb', line 16 def irb_level workspace_stack.size end |
#main ⇒ Object
The top-level workspace, see WorkSpace#main
107 108 109 |
# File 'lib/irb/context.rb', line 107 def main @workspace.main end |
#pop_workspace ⇒ Object
Removes the last element from the current #workspaces stack and returns it, or nil
if the current workspace stack is empty.
Also, see #push_workspace.
57 58 59 60 61 62 63 |
# File 'lib/irb/ext/workspaces.rb', line 57 def pop_workspace if workspaces.empty? print "workspace stack empty\n" return end @workspace = workspaces.pop end |
#prompting? ⇒ Boolean
Whether #verbose? is true
, and input_method
is either StdioInputMethod or ReadlineInputMethod, see #io for more information.
252 253 254 255 |
# File 'lib/irb/context.rb', line 252 def prompting? verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) || (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod))) end |
#push_workspace(*_main) ⇒ Object
Creates a new workspace with the given object or binding, and appends it onto the current #workspaces stack.
See IRB::Context#change_workspace and IRB::WorkSpace.new for more information.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/irb/ext/workspaces.rb', line 34 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_history ⇒ Object
A copy of the default IRB.conf[:SAVE_HISTORY]
26 27 28 |
# File 'lib/irb/ext/save-history.rb', line 26 def save_history IRB.conf[:SAVE_HISTORY] end |
#save_history=(val) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/irb/ext/save-history.rb', line 39 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
Sets the return value from the last statement evaluated in this context to #last_value.
262 263 264 265 |
# File 'lib/irb/context.rb', line 262 def set_last_value(value) @last_value = value @workspace.evaluate self, "_ = IRB.CurrentContext.last_value" end |
#use_loader ⇒ Object Also known as: use_loader?
Returns whether irb
‘s own file reader method is used by load
/require
or not.
This mode is globally affected (irb-wide).
40 41 42 |
# File 'lib/irb/ext/use-loader.rb', line 40 def use_loader IRB.conf[:USE_LOADER] end |
#use_loader=(opt) ⇒ Object
Sets IRB.conf
See #use_loader for more information.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/irb/ext/use-loader.rb', line 49 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 whether messages are displayed or not.
236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/irb/context.rb', line 236 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 |
#workspaces ⇒ Object
WorkSpaces in the current stack
21 22 23 24 25 26 27 |
# File 'lib/irb/ext/workspaces.rb', line 21 def workspaces if defined? @workspaces @workspaces else @workspaces = [] end end |