class IRB::Context
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/irb/context.rbs class IRB::Context def verbose?: () -> false end
configuration of IRB.conf.
A class that wraps the current state of the irb session, including the
def change_workspace(*_main)
object,
IRB.conf[:MAIN_CONTEXT]
when irb was initialized.#home_workspace which is inherited from +TOPLEVEL_BINDING+ or the main
If the optional argument is omitted, the workspace will be
Changes the current workspace to given object or binding.
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
def eval_history=(no)
EvalHistory values are available via
__
variable, seeIf +no+ is +nil+, execution result history isn't used (default).
If +no+ is 0, the number of history items is unlimited.
Returns +no+ of history items if greater than 0.
+no+ is an Integer or +nil+.
IRB.conf[:EVAL_HISTORY]
.Sets command result history limit. Default value is set from
def eval_history=(no) if no if defined?(@eval_history) && @eval_history @eval_history_values.size(no) else @eval_history_values = EvalHistory.new(no) IRB.conf[:__TMP__EHV__] = @eval_history_values @workspace.evaluate("__ = IRB.conf[:__TMP__EHV__]") IRB.conf.delete(:__TMP_EHV__) end else @eval_history_values = nil end @eval_history = no end
def evaluate(line, line_no) # :nodoc:
def evaluate(line, line_no) # :nodoc: @line_no = line_no 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(line, irb_path, line_no) end IRB.conf[:MEASURE_CALLBACKS].inject(last_proc) do |chain, item| _name, callback, arg = item proc do callback.(self, line, line_no, arg) do chain.call end end end.call else result = @workspace.evaluate(line, irb_path, line_no) end set_last_value(result) end
def exit(ret = 0)
def exit(ret = 0) IRB.irb_exit(@irb, ret) rescue UncaughtThrowError super end
def file_input?
Whether #io uses a File for the +input_method+ passed when creating the
def file_input? @io.class == FileInputMethod end
def history_file
IRB.conf[:HISTORY_FILE]
def history_file IRB.conf[:HISTORY_FILE] end
def history_file=(hist)
IRB.conf[:HISTORY_FILE]
to the given +hist+.
def history_file=(hist) IRB.conf[:HISTORY_FILE] = hist end
def home_workspace
def home_workspace if defined? @home_workspace @home_workspace else @home_workspace = @workspace end end
def initialize(irb, workspace = nil, input_method = nil)
+String+:: uses a File
+nil+:: uses stdin or Reline or Readline
The optional +input_method+ argument:
Creates a new IRB context.
def initialize(irb, workspace = nil, input_method = nil) @irb = irb if workspace @workspace = workspace else @workspace = 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] 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 @io = nil case use_multiline? when nil if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline? # Both of multiline mode and singleline mode aren't specified. @io = RelineInputMethod.new else @io = nil end when false @io = nil when true @io = RelineInputMethod.new end unless @io case use_singleline? when nil if (defined?(ReadlineInputMethod) && STDIN.tty? && 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 = '-' @irb_path = '-' 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] @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] end
def inspect # :nodoc:
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
def inspect?
def inspect? @inspect_mode.nil? or @inspect_mode end
def inspect_last_value # :nodoc:
def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end
def inspect_mode=(opt)
options.
Can also be set using the +--inspect+ and +--noinspect+ command line
See IRB::Inspector for more information.
non-inspect mode in math mode
+nil+:: inspect mode in non-math mode,
+false+:: display +to_s+
+true+:: display +inspect+
Specifies the inspect mode with +opt+:
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
def irb_level
def irb_level workspace_stack.size end
def local_variables # :nodoc:
def local_variables # :nodoc: workspace.binding.local_variables end
def main
def main @workspace.main end
def pop_workspace
it, or +nil+ if the current workspace stack is empty.
Removes the last element from the current #workspaces stack and returns
def pop_workspace if workspaces.empty? print "workspace stack empty\n" return end @workspace = workspaces.pop end
def prompt_mode=(mode)
Sets the +mode+ of the prompt in this context.
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] @prompt_n = pconf[:PROMPT_N] @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
def prompting?
StdioInputMethod or RelineInputMethod or ReadlineInputMethod, see #io
Whether #verbose? is +true+, and +input_method+ is either
def prompting? verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) || @io.kind_of?(RelineInputMethod) || (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod))) end
def push_workspace(*_main)
See IRB::Context#change_workspace and IRB::WorkSpace.new for more
onto the current #workspaces stack.
Creates a new workspace with the given object or binding, and appends it
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
def save_history
def save_history IRB.conf[:SAVE_HISTORY] end
def save_history=(val)
def save_history=(val) IRB.conf[:SAVE_HISTORY] = val if val context = (IRB.conf[:MAIN_CONTEXT] || self) if context.io.support_history_saving? && !context.io.singleton_class.include?(HistorySavingAbility) context.io.extend(HistorySavingAbility) end end end
def set_last_value(value)
Sets the return value from the last statement evaluated in this context
def set_last_value(value) @last_value = value @workspace.local_variable_set :_, value end
def set_last_value(value)
def set_last_value(value) _set_last_value(value) if defined?(@eval_history) && @eval_history @eval_history_values.push @line_no, @last_value @workspace.evaluate "__ = IRB.CurrentContext.instance_eval{@eval_history_values}" end @last_value end
def symbol_alias?(command)
def symbol_alias?(command) return nil if command.match?(/\A\w+\z/) command_aliases.key?(command.to_sym) end
def transform_args?(command)
def transform_args?(command) command = command_aliases.fetch(command.to_sym, command) ExtendCommandBundle.load_command(command)&.respond_to?(:transform_args) end
def use_loader
+load+/+require+ or not.
Returns whether +irb+'s own file reader method is used by
def use_loader IRB.conf[:USE_LOADER] end
def use_loader=(opt)
Sets
IRB.conf[:USE_LOADER]
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
def use_tracer=(opt)
def use_tracer=(opt) # do nothing end
def use_tracer=(opt)
in this context.
Sets whether or not to use the Tracer library when evaluating statements
def use_tracer=(opt) if opt Tracer.set_get_line_procs(@irb_path) { |line_no, *rests| @io.line(line_no) } elsif !opt && @use_tracer Tracer.off end @use_tracer=opt end
def verbose?
Experimental RBS support (using type sampling data from the type_fusion
project).
def verbose?: () -> false
This signature was generated using 1 sample from 1 application.
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
def workspaces
def workspaces if defined? @workspaces @workspaces else @workspaces = [] end end