Class: IRB::Context
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/irb/context.rb, lib/irb/ext/change-ws.rb, lib/irb/ext/eval_history.rb, lib/irb/ext/use-loader.rb, lib/irb/ext/workspaces.rb |
Overview
A class that wraps the current state of the irb session, including the configuration of conf.
Constant Summary
-
ASSIGN_OPERATORS_REGEXP =
# File 'lib/irb/context.rb', line 19Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=])
-
IDNAME_IVARS =
Internal use only
# File 'lib/irb/context.rb', line 671["@prompt_mode"] -
KERNEL_METHOD =
# File 'lib/irb/context.rb', line 17::Kernel.instance_method(:method)
-
KERNEL_PUBLIC_METHOD =
# File 'lib/irb/context.rb', line 16::Kernel.instance_method(:public_method)
-
NOPRINTING_IVARS =
Internal use only
# File 'lib/irb/context.rb', line 669["@last_value"] -
NO_INSPECTING_IVARS =
Internal use only
# File 'lib/irb/context.rb', line 670["@irb", "@io"]
Class Method Summary
-
.new(irb, workspace = nil, input_method = nil) ⇒ Context
constructor
Creates a new
::IRBcontext.
Instance Attribute Summary
-
#ap_name
rw
A copy of the default
IRB.conf[:AP_NAME]. -
#auto_indent_mode
rw
Can be either the default
IRB.conf[:AUTO_INDENT], or the mode set by #prompt_mode= -
#back_trace_limit
rw
The limit of backtrace lines displayed as top
nand tailn. -
#command_aliases
rw
User-defined
::IRBcommand aliases. -
#echo?
rw
Alias for #echo.
-
#echo_on_assignment?
rw
Alias for #echo_on_assignment.
-
#eval_history
rw
The command result history limit.
-
#eval_history=(no)
rw
Sets command result history limit.
-
#extra_doc_dirs
rw
Specify the installation locations of the ri file to be displayed in the document dialog.
- #file_input? ⇒ Boolean readonly
- #from_binding? ⇒ Boolean readonly
-
#history_file
rw
A copy of the default
IRB.conf[:HISTORY_FILE]. -
#history_file=(hist)
rw
Set
IRB.conf[:HISTORY_FILE]to the givenhist. -
#ignore_eof?
rw
Alias for #ignore_eof.
-
#ignore_sigint?
rw
Alias for #ignore_sigint.
-
#inspect? ⇒ Boolean
readonly
Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
-
#inspect_method
readonly
Inspectorfor the current context. -
#inspect_mode
rw
A copy of the default
IRB.conf[:INSPECT_MODE]. -
#inspect_mode=(opt)
rw
Specifies the inspect mode with
opt: - #inspector_support_stream_output? ⇒ Boolean readonly
-
#io
rw
The current input method.
-
#irb
rw
Current irb session.
-
#irb_name
rw
Can be either name from
IRB.conf[:IRB_NAME], or the number of the current job set byJobManager, such asirb#2. - #irb_path rw
- #irb_path=(path) rw
-
#last_value
readonly
The return value of the last statement evaluated.
-
#load_modules
rw
A copy of the default
IRB.conf[:LOAD_MODULES]. -
#newline_before_multiline_output?
rw
Alias for #newline_before_multiline_output.
-
#prompt_c
rw
::IRBprompt for continuated statement. -
#prompt_i
rw
Standard IRB prompt.
-
#prompt_mode
rw
A copy of the default
IRB.conf[:PROMPT_MODE]. -
#prompt_mode=(mode)
rw
Sets the
modeof the prompt in this context. -
#prompt_n
rw
TODO: Remove this when developing v2.0.
-
#prompt_n=(_)
rw
TODO: Remove this when developing v2.0.
-
#prompt_s
rw
::IRBprompt for continuated strings. -
#prompting? ⇒ Boolean
readonly
Whether #verbose? is
true, andinput_methodis eitherStdioInputMethodorRelineInputMethodorReadlineInputMethod, see #io for more information. -
#rc?
rw
Alias for #rc.
-
#return_format
rw
The format of the return statement, set by #prompt_mode= using the
:RETURNof themodepassed to set the current #prompt_mode. - #save_history rw
- #save_history=(val) rw
-
#thread
readonly
The current thread in this context.
-
#use_autocomplete?
readonly
Alias for #use_autocomplete.
-
#use_loader=(opt)
rw
Sets
IRB.conf[:USE_LOADER]. -
#use_loader?
rw
Alias for #use_loader.
-
#use_multiline?
readonly
Alias for #use_multiline.
-
#use_readline
readonly
Alias for #use_singleline.
-
#use_readline?
readonly
Alias for #use_singleline.
-
#use_reline
readonly
Alias for #use_multiline.
-
#use_reline?
readonly
Alias for #use_multiline.
-
#use_singleline?
readonly
Alias for #use_singleline.
- #use_tracer=(val) writeonly
-
#verbose
rw
Whether verbose messages are displayed or not.
-
#verbose? ⇒ Boolean
rw
Returns whether messages are displayed or not.
- #with_debugger rw
-
#workspace_home
readonly
The toplevel workspace, see #home_workspace
- #term_interactive? ⇒ Boolean readonly private
Instance Method Summary
- #__inspect__
- #__to_s__
-
#_set_last_value(value)
Alias for #set_last_value.
-
#change_workspace(*_main)
Changes the current workspace to given object or binding.
- #colorize_input(input, complete:)
-
#echo
(also: #echo?)
rw
Whether to echo the return value to output or not.
-
#echo_on_assignment
(also: #echo_on_assignment?)
rw
Whether to echo for assignment expressions.
-
#home_workspace
Inherited from TOPLEVEL_BINDING.
-
#ignore_eof
(also: #ignore_eof?)
rw
Whether
^D(control-d) will be ignored or not. -
#ignore_sigint
(also: #ignore_sigint?)
rw
Whether
^C(control-c) will be ignored or not. -
#main
The top-level workspace, see WorkSpace#main
-
#newline_before_multiline_output
(also: #newline_before_multiline_output?)
rw
Whether a newline is put before multiline output.
- #parse_input(code, is_assignment_expression)
-
#pop_workspace
Removes the last element from the current
#workspacesstack and returns it, ornilif the current workspace stack is empty. -
#push_workspace(*_main)
Creates a new workspace with the given object or binding, and appends it onto the current
#workspacesstack. -
#rc
(also: #rc?)
rw
A copy of the default
IRB.conf[:RC]. -
#replace_workspace(workspace)
Replace the current workspace with the given #workspace.
- #safe_method_call_on_main(method_name)
-
#set_last_value(value)
(also: #_set_last_value)
Sets the return value from the last statement evaluated in this context to #last_value.
-
#to_s
Alias for #inspect.
-
#use_autocomplete
(also: #use_autocomplete?)
readonly
Whether colorization is enabled or not.
-
#use_loader
(also: #use_loader?)
rw
Returns whether #irb‘s own file reader method is used by
load+/+requireor not. -
#use_multiline
(also: #use_multiline?, #use_reline, #use_reline?)
readonly
Whether multiline editor mode is enabled or not.
-
#use_singleline
(also: #use_singleline?, #use_readline, #use_readline?)
readonly
Whether singleline editor mode is enabled or not.
-
#workspace
Workspace in the current context.
- #build_completor private
- #build_type_completor(verbose:) private
- #evaluate(statement, line_no) Internal use only
- #evaluate_expression(code, line_no) Internal use only
- #inspect (also: #to_s) readonly Internal use only
- #inspect_last_value(output = +'')) Internal use only
- #local_variables Internal use only
Constructor Details
.new(irb, workspace = nil, input_method = nil) ⇒ Context
Creates a new ::IRB context.
The optional input_method argument:
nil-
uses stdin or Reline or Readline
String-
uses a File
other-
uses this as
InputMethod
# File 'lib/irb/context.rb', line 27
def initialize(irb, workspace = nil, input_method = nil) @irb = irb @workspace_stack = [] if workspace @workspace_stack << workspace else @workspace_stack << WorkSpace.new end @thread = Thread.current # copy of default configuration @ap_name = IRB.conf[:AP_NAME] @rc = IRB.conf[:RC] @load_modules = IRB.conf[:LOAD_MODULES] if IRB.conf.has_key?(:USE_SINGLELINE) @use_singleline = IRB.conf[:USE_SINGLELINE] elsif IRB.conf.has_key?(:USE_READLINE) # backward compatibility @use_singleline = IRB.conf[:USE_READLINE] else @use_singleline = nil end if IRB.conf.has_key?(:USE_MULTILINE) @use_multiline = IRB.conf[:USE_MULTILINE] elsif IRB.conf.has_key?(:USE_RELINE) # backward compatibility warn <<~MSG.strip USE_RELINE is deprecated, please use USE_MULTILINE instead. MSG @use_multiline = IRB.conf[:USE_RELINE] elsif IRB.conf.has_key?(:USE_REIDLINE) warn <<~MSG.strip USE_REIDLINE is deprecated, please use USE_MULTILINE instead. MSG @use_multiline = IRB.conf[:USE_REIDLINE] else @use_multiline = nil end @use_autocomplete = IRB.conf[:USE_AUTOCOMPLETE] @verbose = IRB.conf[:VERBOSE] @io = nil self.inspect_mode = IRB.conf[:INSPECT_MODE] self.use_tracer = 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] @irb_name = IRB.conf[:IRB_NAME] unless IRB.conf[:SINGLE_IRB] or !defined?(IRB::JobManager) @irb_name = @irb_name + "#" + IRB.JobManager.n_jobs.to_s end self.irb_path = "(" + @irb_name + ")" case input_method when nil @io = nil case use_multiline? when nil if term_interactive? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline? # Both of multiline mode and singleline mode aren't specified. @io = RelineInputMethod.new(build_completor) else @io = nil end when false @io = nil when true @io = RelineInputMethod.new(build_completor) end unless @io case use_singleline? when nil if (defined?(ReadlineInputMethod) && term_interactive? && IRB.conf[:PROMPT_MODE] != :INF_RUBY) @io = ReadlineInputMethod.new else @io = nil end when false @io = nil when true if defined?(ReadlineInputMethod) @io = ReadlineInputMethod.new else @io = nil end else @io = nil end end @io = StdioInputMethod.new unless @io when '-' @io = FileInputMethod.new($stdin) @irb_name = '-' self.irb_path = '-' when String @io = FileInputMethod.new(input_method) @irb_name = File.basename(input_method) self.irb_path = input_method else @io = input_method end @extra_doc_dirs = IRB.conf[:EXTRA_DOC_DIRS] @echo = IRB.conf[:ECHO] if @echo.nil? @echo = true end @echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT] if @echo_on_assignment.nil? @echo_on_assignment = :truncate end @newline_before_multiline_output = IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] if @newline_before_multiline_output.nil? @newline_before_multiline_output = true end @command_aliases = IRB.conf[:COMMAND_ALIASES].dup end
Instance Attribute Details
#ap_name (rw)
A copy of the default IRB.conf[:AP_NAME]
# File 'lib/irb/context.rb', line 223
attr_accessor :ap_name
#auto_indent_mode (rw)
Can be either the default IRB.conf[:AUTO_INDENT], or the mode set by #prompt_mode=
To disable auto-indentation in irb:
IRB.conf[:AUTO_INDENT] = false
or
irb_context.auto_indent_mode = false
or
IRB.CurrentContext.auto_indent_mode = false
See IRB@Configuration for more information.
# File 'lib/irb/context.rb', line 313
attr_accessor :auto_indent_mode
#back_trace_limit (rw)
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.
# File 'lib/irb/context.rb', line 404
attr_accessor :back_trace_limit
#command_aliases (rw)
User-defined ::IRB command aliases
# File 'lib/irb/context.rb', line 407
attr_accessor :command_aliases
#echo? (rw)
Alias for #echo.
# File 'lib/irb/context.rb', line 429
alias echo? echo
#echo_on_assignment? (rw)
Alias for #echo_on_assignment.
# File 'lib/irb/context.rb', line 430
alias echo_on_assignment? echo_on_assignment
#eval_history (rw)
The command result history limit. This method is not available until #eval_history= was called with non-nil value (directly or via setting IRB.conf[:EVAL_HISTORY] in .irbrc).
# File 'lib/irb/ext/eval_history.rb', line 31
attr_reader :eval_history
#eval_history=(no) (rw)
Sets command result history limit. Default value is set from IRB.conf[:EVAL_HISTORY].
no is an Integer or nil.
Returns no of history items if greater than 0.
If no is 0, the number of history items is unlimited.
If no is nil, execution result history isn’t used (default).
EvalHistory values are available via __ variable, see EvalHistory.
# File 'lib/irb/ext/eval_history.rb', line 45
def eval_history=(val) self.class.remove_method(__method__) require_relative "ext/eval_history" __send__(__method__, val) end
#extra_doc_dirs (rw)
Specify the installation locations of the ri file to be displayed in the document dialog.
# File 'lib/irb/context.rb', line 333
attr_accessor :extra_doc_dirs
#file_input? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/irb/context.rb', line 492
def file_input? @io.class == FileInputMethod end
#from_binding? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/irb/context.rb', line 568
def from_binding? @irb.from_binding end
#history_file (rw)
A copy of the default IRB.conf[:HISTORY_FILE]
#history_file=(hist) (rw)
Set IRB.conf[:HISTORY_FILE] to the given hist.
#ignore_eof? (rw)
Alias for #ignore_eof.
# File 'lib/irb/context.rb', line 428
alias ignore_eof? ignore_eof
#ignore_sigint? (rw)
Alias for #ignore_sigint.
# File 'lib/irb/context.rb', line 427
alias ignore_sigint? ignore_sigint
#inspect? ⇒ Boolean (readonly)
Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
# File 'lib/irb/context.rb', line 486
def inspect? @inspect_mode.nil? or @inspect_mode end
#inspect_method (readonly)
Inspector for the current context
# File 'lib/irb/context.rb', line 268
attr_reader :inspect_method
#inspect_mode (rw)
A copy of the default IRB.conf[:INSPECT_MODE]
# File 'lib/irb/context.rb', line 266
attr_reader :inspect_mode
#inspect_mode=(opt) (rw)
# File 'lib/irb/context.rb', line 507
def inspect_mode=(opt) if i = Inspector::INSPECTORS[opt] @inspect_mode = opt @inspect_method = i i.init else case opt when nil if Inspector.keys_with_inspector(Inspector::INSPECTORS[true]).include?(@inspect_mode) self.inspect_mode = false elsif Inspector.keys_with_inspector(Inspector::INSPECTORS[false]).include?(@inspect_mode) self.inspect_mode = true else puts "Can't switch inspect mode." return end when /^\s*\{.*\}\s*$/ begin inspector = eval "proc#{opt}" rescue Exception puts "Can't switch inspect mode(#{opt})." return end self.inspect_mode = inspector when Proc self.inspect_mode = IRB::Inspector(opt) when Inspector prefix = "usr%d" i = 1 while Inspector::INSPECTORS[format(prefix, i)]; i += 1; end @inspect_mode = format(prefix, i) @inspect_method = opt Inspector.def_inspector(format(prefix, i), @inspect_method) else puts "Can't switch inspect mode(#{opt})." return end end print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose? @inspect_mode end
#inspector_support_stream_output? ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/irb/context.rb', line 665
def inspector_support_stream_output? @inspect_method.support_stream_output? end
#io (rw)
The current input method.
Can be either StdioInputMethod, ReadlineInputMethod, RelineInputMethod, FileInputMethod or other specified when the context is created. See .new for more # information on input_method.
# File 'lib/irb/context.rb', line 218
attr_accessor :io
#irb (rw)
Current irb session.
# File 'lib/irb/context.rb', line 221
attr_accessor :irb
#irb_name (rw)
Can be either name from IRB.conf[:IRB_NAME], or the number of the current job set by JobManager, such as irb#2
# File 'lib/irb/context.rb', line 230
attr_accessor :irb_name
#irb_path (rw)
# File 'lib/irb/context.rb', line 236
attr_reader :irb_path
#irb_path=(path) (rw)
#last_value (readonly)
The return value of the last statement evaluated.
# File 'lib/irb/context.rb', line 458
attr_reader :last_value
#load_modules (rw)
A copy of the default IRB.conf[:LOAD_MODULES]
# File 'lib/irb/context.rb', line 227
attr_accessor :load_modules
#newline_before_multiline_output? (rw)
Alias for #newline_before_multiline_output.
# File 'lib/irb/context.rb', line 431
alias newline_before_multiline_output? newline_before_multiline_output
#prompt_c (rw)
::IRB prompt for continuated statement. (e.g. immediately after an if)
See Custom Prompts for more information.
# File 'lib/irb/context.rb', line 283
attr_accessor :prompt_c
#prompt_i (rw)
Standard IRB prompt.
See Custom Prompts for more information.
# File 'lib/irb/context.rb', line 275
attr_accessor :prompt_i
#prompt_mode (rw)
A copy of the default IRB.conf[:PROMPT_MODE]
# File 'lib/irb/context.rb', line 271
attr_reader :prompt_mode
#prompt_mode=(mode) (rw)
Sets the mode of the prompt in this context.
See Custom Prompts for more information.
# File 'lib/irb/context.rb', line 470
def prompt_mode=(mode) @prompt_mode = mode pconf = IRB.conf[:PROMPT][mode] @prompt_i = pconf[:PROMPT_I] @prompt_s = pconf[:PROMPT_S] @prompt_c = pconf[:PROMPT_C] @return_format = pconf[:RETURN] @return_format = "%s\n" if @return_format == nil if ai = pconf.include?(:AUTO_INDENT) @auto_indent_mode = ai else @auto_indent_mode = IRB.conf[:AUTO_INDENT] end end
#prompt_n (rw)
TODO: Remove this when developing v2.0
# File 'lib/irb/context.rb', line 286
def prompt_n warn "IRB::Context#prompt_n is deprecated and will be removed in the next major release." "" end
#prompt_n=(_) (rw)
TODO: Remove this when developing v2.0
# File 'lib/irb/context.rb', line 292
def prompt_n=(_) warn "IRB::Context#prompt_n= is deprecated and will be removed in the next major release." "" end
#prompt_s (rw)
::IRB prompt for continuated strings.
See Custom Prompts for more information.
# File 'lib/irb/context.rb', line 279
attr_accessor :prompt_s
#prompting? ⇒ Boolean (readonly)
Whether #verbose? is true, and input_method is either StdioInputMethod or RelineInputMethod or ReadlineInputMethod, see #io for more information.
# File 'lib/irb/context.rb', line 453
def prompting? verbose? || @io.prompting? end
#rc? (rw)
Alias for #rc.
# File 'lib/irb/context.rb', line 426
alias rc? rc
#return_format (rw)
The format of the return statement, set by #prompt_mode= using the :RETURN of the mode passed to set the current #prompt_mode.
# File 'lib/irb/context.rb', line 316
attr_accessor :return_format
#save_history (rw)
[ GitHub ]#save_history=(val) (rw)
[ GitHub ]
#term_interactive? ⇒ Boolean (readonly, private)
[ GitHub ]
# File 'lib/irb/context.rb', line 707
def term_interactive? return true if ENV['TEST_IRB_FORCE_INTERACTIVE'] STDIN.tty? && ENV['TERM'] != 'dumb' end
#thread (readonly)
The current thread in this context.
# File 'lib/irb/context.rb', line 212
attr_reader :thread
#use_autocomplete? (readonly)
Alias for #use_autocomplete.
# File 'lib/irb/context.rb', line 424
alias use_autocomplete? use_autocomplete
#use_loader=(opt) (rw)
Sets IRB.conf[:USE_LOADER]
See #use_loader for more information.
# File 'lib/irb/ext/use-loader.rb', line 47
def use_loader=(val) self.class.remove_method(__method__) require_relative "ext/use-loader" __send__(__method__, val) end
#use_loader? (rw)
Alias for #use_loader.
# File 'lib/irb/ext/use-loader.rb', line 41
alias use_loader? use_loader
#use_multiline? (readonly)
Alias for #use_multiline.
# File 'lib/irb/context.rb', line 412
alias use_multiline? use_multiline
#use_readline (readonly)
Alias for #use_singleline.
# File 'lib/irb/context.rb', line 420
alias use_readline use_singleline
#use_readline? (readonly)
Alias for #use_singleline.
# File 'lib/irb/context.rb', line 422
alias use_readline? use_singleline
#use_reline (readonly)
Alias for #use_multiline.
# File 'lib/irb/context.rb', line 416
alias use_reline use_multiline
#use_reline? (readonly)
Alias for #use_multiline.
# File 'lib/irb/context.rb', line 418
alias use_reline? use_multiline
#use_singleline? (readonly)
Alias for #use_singleline.
# File 'lib/irb/context.rb', line 414
alias use_singleline? use_singleline
#use_tracer=(val) (writeonly)
[ GitHub ]#verbose (rw)
Whether verbose messages are displayed or not.
A copy of the default IRB.conf[:VERBOSE]
# File 'lib/irb/context.rb', line 397
attr_accessor :verbose
#verbose? ⇒ Boolean (rw)
Returns whether messages are displayed or not.
# File 'lib/irb/context.rb', line 434
def verbose? if @verbose.nil? if @io.kind_of?(RelineInputMethod) false elsif defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod) false elsif !STDIN.tty? or @io.kind_of?(FileInputMethod) true else false end else @verbose end end
#with_debugger (rw)
[ GitHub ]# File 'lib/irb/context.rb', line 409
attr_accessor :with_debugger
#workspace_home (readonly)
The toplevel workspace, see #home_workspace
# File 'lib/irb/context.rb', line 210
attr_reader :workspace_home
Instance Method Details
#__inspect__
[ GitHub ]# File 'lib/irb/context.rb', line 673
alias __inspect__ inspect
#__to_s__
[ GitHub ]# File 'lib/irb/context.rb', line 693
alias __to_s__ to_s
#_set_last_value(value)
Alias for #set_last_value.
# File 'lib/irb/ext/eval_history.rb', line 14
alias _set_last_value set_last_value
#build_completor (private)
[ GitHub ]# File 'lib/irb/context.rb', line 712
def build_completor completor_type = IRB.conf[:COMPLETOR] # Gem repl_type_completor is added to bundled gems in Ruby 3.4. # Use :type as default completor only in Ruby 3.4 or later. verbose = !!completor_type completor_type ||= RUBY_VERSION >= '3.4' ? :type : :regexp case completor_type when :regexp return RegexpCompletor.new when :type completor = build_type_completor(verbose: verbose) return completor if completor else warn "Invalid value for IRB.conf[:COMPLETOR]: #{completor_type}" end # Fallback to RegexpCompletor RegexpCompletor.new end
#build_type_completor(verbose:) (private)
[ GitHub ]# File 'lib/irb/context.rb', line 733
def build_type_completor(verbose:) if RUBY_ENGINE == 'truffleruby' # Avoid SyntaxError. truffleruby does not support endless method definition yet. warn 'TypeCompletor is not supported on TruffleRuby yet' if verbose return end begin require 'repl_type_completor' rescue LoadError => e warn "TypeCompletor requires `gem repl_type_completor`: #{e.}" if verbose return end ReplTypeCompletor.preload_rbs TypeCompletor.new(self) end
#change_workspace(*_main)
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 WorkSpace.new for more information.
# File 'lib/irb/ext/change-ws.rb', line 26
def change_workspace(*_main) if _main.empty? replace_workspace(home_workspace) return main end workspace = WorkSpace.new(_main[0]) replace_workspace(workspace) workspace.load_helper_methods_to_main end
#colorize_input(input, complete:)
[ GitHub ]# File 'lib/irb/context.rb', line 645
def colorize_input(input, complete:) if IRB.conf[:USE_COLORIZE] && IRB::Color.colorable? lvars = local_variables || [] parsed_input = parse_input(input, false) if parsed_input.is_a?(Statement::Command) name, sep, arg = input.split(/(\s+)/, 2) arg = IRB::Color.colorize_code(arg, complete: complete, local_variables: lvars) "#{IRB::Color.colorize(name, [:BOLD])}\e[m#{sep}#{arg}" else IRB::Color.colorize_code(input, complete: complete, local_variables: lvars) end else Reline::Unicode.escape_for_print(input) end end
#echo (rw) Also known as: #echo?
Whether to echo the return value to output or not.
Uses IRB.conf[:ECHO] if available, or defaults to true.
puts "hello"
# hello
#=> nil
IRB.CurrentContext.echo = false
puts "omg"
# omg
# File 'lib/irb/context.rb', line 344
attr_accessor :echo
#echo_on_assignment (rw) Also known as: #echo_on_assignment?
Whether to echo for assignment expressions.
If set to false, the value of assignment will not be shown.
If set to true, the value of assignment will be shown.
If set to :truncate, the value of assignment will be shown and truncated.
It defaults to :truncate.
a = "omg"
#=> omg
a = "omg" * 10
#=> omgomgomgomgomgomgomg...
IRB.CurrentContext.echo_on_assignment = false
a = "omg"
IRB.CurrentContext.echo_on_assignment = true
a = "omg" * 10
#=> omgomgomgomgomgomgomgomgomgomg
To set the behaviour of showing on assignment in irb:
IRB.conf[:ECHO_ON_ASSIGNMENT] = :truncate or true or false
or
irb_context.echo_on_assignment = :truncate or true or false
or
IRB.CurrentContext.echo_on_assignment = :truncate or true or false
# File 'lib/irb/context.rb', line 379
attr_accessor :echo_on_assignment
#evaluate(statement, line_no)
# File 'lib/irb/context.rb', line 550
def evaluate(statement, line_no) # :nodoc: @line_no = line_no case statement when Statement::EmptyInput return when Statement::Expression result = evaluate_expression(statement.code, line_no) set_last_value(result) when Statement::Command statement.command_class.execute(self, statement.arg) when Statement::IncorrectAlias warn statement. end nil end
#evaluate_expression(code, line_no)
# File 'lib/irb/context.rb', line 572
def evaluate_expression(code, line_no) # :nodoc: result = nil if IRB.conf[:MEASURE] && IRB.conf[:MEASURE_CALLBACKS].empty? IRB.set_measure_callback end if IRB.conf[:MEASURE] && !IRB.conf[:MEASURE_CALLBACKS].empty? last_proc = proc do result = workspace.evaluate(code, @eval_path, line_no) end IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item| _name, callback, arg = item proc do callback.(self, code, line_no, arg) do chain.call end end end.call else result = workspace.evaluate(code, @eval_path, line_no) end result end
#home_workspace
Inherited from TOPLEVEL_BINDING.
# File 'lib/irb/ext/change-ws.rb', line 11
def home_workspace if defined? @home_workspace @home_workspace else @home_workspace = workspace end end
#ignore_eof (rw) Also known as: #ignore_eof?
Whether ^D (control-d) will be ignored or not.
If set to false, ^D will quit irb.
# File 'lib/irb/context.rb', line 330
attr_accessor :ignore_eof
#ignore_sigint (rw) 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.
# File 'lib/irb/context.rb', line 326
attr_accessor :ignore_sigint
#inspect (readonly) Also known as: #to_s
# File 'lib/irb/context.rb', line 674
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_last_value(output = +''))
# File 'lib/irb/context.rb', line 661
def inspect_last_value(output = +'') # :nodoc: @inspect_method.inspect_value(@last_value, output) end
#local_variables
# File 'lib/irb/context.rb', line 696
def local_variables # :nodoc: workspace.binding.local_variables end
#main
The top-level workspace, see WorkSpace#main
# File 'lib/irb/context.rb', line 205
def main workspace.main end
#newline_before_multiline_output (rw) Also known as: #newline_before_multiline_output?
Whether a newline is put before multiline output.
Uses IRB.conf[:NEWLINE_BEFORE_MULTILINE_OUTPUT] if available, or defaults to true.
"abc\ndef"
#=>
abc
def
IRB.CurrentContext.newline_before_multiline_output = false
"abc\ndef"
#=> abc
def
# File 'lib/irb/context.rb', line 393
attr_accessor :newline_before_multiline_output
#parse_input(code, is_assignment_expression)
[ GitHub ]# File 'lib/irb/context.rb', line 596
def parse_input(code, is_assignment_expression) command_name, arg = code.strip.split(/\s+/, 2) arg ||= '' # command can only be 1 line if code.lines.size != 1 || # command name is required command_name.nil? || # local variable have precedence over command local_variables.include?(command_name.to_sym) || # assignment expression is not a command (is_assignment_expression || (arg.start_with?(ASSIGN_OPERATORS_REGEXP) && !arg.start_with?(/==|=~/))) return Statement::Expression.new(code, is_assignment_expression) end command = command_name.to_sym # Check command aliases if aliased_name = command_aliases[command] if command_class = Command.load_command(aliased_name) command = aliased_name elsif HelperMethod.helper_methods[aliased_name] = <<~MESSAGE Using command alias `#{command}` for helper method `#{aliased_name}` is not supported. Please check the value of `IRB.conf[:COMMAND_ALIASES]`. MESSAGE return Statement::IncorrectAlias.new() else = <<~MESSAGE You're trying to use command alias `#{command}` for command `#{aliased_name}`, but `#{aliased_name}` does not exist. Please check the value of `IRB.conf[:COMMAND_ALIASES]`. MESSAGE return Statement::IncorrectAlias.new() end else command_class = Command.load_command(command) end # Check visibility public_method = !!KERNEL_PUBLIC_METHOD.bind_call(main, command) rescue false private_method = !public_method && !!KERNEL_METHOD.bind_call(main, command) rescue false if command_class && Command.execute_as_command?(command, public_method: public_method, private_method: private_method) Statement::Command.new(code, command_class, arg) else Statement::Expression.new(code, is_assignment_expression) end end
#pop_workspace
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.
# File 'lib/irb/ext/workspaces.rb', line 32
def pop_workspace @workspace_stack.pop if @workspace_stack.size > 1 end
#push_workspace(*_main)
Creates a new workspace with the given object or binding, and appends it onto the current #workspaces stack.
See #change_workspace and WorkSpace.new for more information.
# File 'lib/irb/ext/workspaces.rb', line 14
def push_workspace(*_main) if _main.empty? if @workspace_stack.size > 1 # swap the top two workspaces previous_workspace, current_workspace = @workspace_stack.pop(2) @workspace_stack.push current_workspace, previous_workspace end else new_workspace = WorkSpace.new(workspace.binding, _main[0]) @workspace_stack.push new_workspace new_workspace.load_helper_methods_to_main end end
#rc (rw) Also known as: #rc?
A copy of the default IRB.conf[:RC]
# File 'lib/irb/context.rb', line 225
attr_accessor :rc
#replace_workspace(workspace)
Replace the current workspace with the given #workspace.
#safe_method_call_on_main(method_name)
[ GitHub ]#set_last_value(value) Also known as: #_set_last_value
Sets the return value from the last statement evaluated in this context to #last_value.
See additional method definition at file lib/irb/context.rb line 462.
# File 'lib/irb/ext/eval_history.rb', line 16
def set_last_value(value) @last_value = value workspace.local_variable_set :_, value end
#to_s
Alias for #inspect.
# File 'lib/irb/context.rb', line 694
alias to_s inspect
#use_autocomplete (readonly) Also known as: #use_autocomplete?
Whether colorization is enabled or not.
A copy of the default IRB.conf[:USE_AUTOCOMPLETE]
# File 'lib/irb/context.rb', line 264
attr_reader :use_autocomplete
#use_loader (rw) 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).
#use_multiline (readonly) Also known as: #use_multiline?, #use_reline, #use_reline?
Whether multiline editor mode is enabled or not.
A copy of the default IRB.conf[:USE_MULTILINE]
# File 'lib/irb/context.rb', line 256
attr_reader :use_multiline
#use_singleline (readonly) Also known as: #use_singleline?, #use_readline, #use_readline?
Whether singleline editor mode is enabled or not.
A copy of the default IRB.conf[:USE_SINGLELINE]
# File 'lib/irb/context.rb', line 260
attr_reader :use_singleline
#workspace
Workspace in the current context.
# File 'lib/irb/context.rb', line 194
def workspace @workspace_stack.last end