123456789_123456789_123456789_123456789_123456789_

Class: IRB::Context

Overview

A class that wraps the current state of the irb session, including the configuration of conf.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

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

[ GitHub ]

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

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]

[ GitHub ]

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

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.

[ GitHub ]

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

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.

[ GitHub ]

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

attr_accessor :back_trace_limit

#command_aliases (rw)

User-defined ::IRB command aliases

[ GitHub ]

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

attr_accessor :command_aliases

#echo? (rw)

Alias for #echo.

[ GitHub ]

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

alias echo? echo

#echo_on_assignment? (rw)

Alias for #echo_on_assignment.

[ GitHub ]

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

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

[ GitHub ]

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

[ GitHub ]

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

[ GitHub ]

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

attr_accessor :extra_doc_dirs

#file_input?Boolean (readonly)

Whether #io uses a File for the input_method passed when creating the current context, see .new

[ GitHub ]

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

def file_input?
  @io.class == FileInputMethod
end

#from_binding?Boolean (readonly)

[ GitHub ]

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

def from_binding?
  @irb.from_binding
end

#history_file (rw)

A copy of the default IRB.conf[:HISTORY_FILE]

[ GitHub ]

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

def history_file
  IRB.conf[:HISTORY_FILE]
end

#history_file=(hist) (rw)

Set IRB.conf[:HISTORY_FILE] to the given hist.

[ GitHub ]

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

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

#ignore_eof? (rw)

Alias for #ignore_eof.

[ GitHub ]

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

alias ignore_eof? ignore_eof

#ignore_sigint? (rw)

Alias for #ignore_sigint.

[ GitHub ]

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

alias ignore_sigint? ignore_sigint

#inspect?Boolean (readonly)

Whether #inspect_mode is set or not, see #inspect_mode= for more detail.

[ GitHub ]

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

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

#inspect_mode (rw)

A copy of the default IRB.conf[:INSPECT_MODE]

[ GitHub ]

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

attr_reader :inspect_mode

#inspect_mode=(opt) (rw)

Specifies the inspect mode with opt:

true

display #inspect

false

display #to_s

nil

inspect mode in non-math mode, non-inspect mode in math mode

See Inspector for more information.

Can also be set using the –inspect and –noinspect command line options.

[ GitHub ]

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

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

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

[ GitHub ]

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

attr_accessor :io

#irb (rw)

Current irb session.

[ GitHub ]

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

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

[ GitHub ]

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

attr_accessor :irb_name

#irb_path (rw)

Can be one of the following:

  • the #irb_name surrounded by parenthesis

  • the input_method passed to .new

  • the file path of the current ::IRB context in a binding.irb session

[ GitHub ]

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

attr_reader :irb_path

#irb_path=(path) (rw)

Sets @irb_path to the given path as well as @eval_path It’s the same as irb_path, but with the ::IRB name postfix This makes sure users can distinguish the methods defined in the ::IRB session from the methods defined in the current file’s context, especially with binding.irb

[ GitHub ]

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

def irb_path=(path)
  @irb_path = path

  if File.exist?(path)
    @eval_path = "#{path}(#{IRB.conf[:IRB_NAME]})"
  else
    @eval_path = path
  end
end

#last_value (readonly)

The return value of the last statement evaluated.

[ GitHub ]

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

attr_reader :last_value

#load_modules (rw)

A copy of the default IRB.conf[:LOAD_MODULES]

[ GitHub ]

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

attr_accessor :load_modules

#newline_before_multiline_output? (rw)

[ GitHub ]

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

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.

[ GitHub ]

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

attr_accessor :prompt_c

#prompt_i (rw)

Standard IRB prompt.

See Custom Prompts for more information.

[ GitHub ]

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

attr_accessor :prompt_i

#prompt_mode (rw)

A copy of the default IRB.conf[:PROMPT_MODE]

[ GitHub ]

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

attr_reader :prompt_mode

#prompt_mode=(mode) (rw)

Sets the mode of the prompt in this context.

See Custom Prompts for more information.

[ GitHub ]

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

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

[ GitHub ]

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

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

[ GitHub ]

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

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.

[ GitHub ]

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

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.

[ GitHub ]

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

def prompting?
  verbose? || @io.prompting?
end

#rc? (rw)

Alias for #rc.

[ GitHub ]

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

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.

[ GitHub ]

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

attr_accessor :return_format

#save_history (rw)

[ GitHub ]

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

def save_history
  IRB.conf[:SAVE_HISTORY]
end

#save_history=(val) (rw)

[ GitHub ]

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

def save_history=(val)
  IRB.conf[:SAVE_HISTORY] = val
end

#term_interactive?Boolean (readonly, private)

[ GitHub ]

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

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

[ GitHub ]

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

attr_reader :thread

#use_autocomplete? (readonly)

Alias for #use_autocomplete.

[ GitHub ]

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

alias use_autocomplete? use_autocomplete

#use_loader=(opt) (rw)

Sets IRB.conf[:USE_LOADER]

See #use_loader for more information.

[ GitHub ]

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

[ GitHub ]

  
# File 'lib/irb/ext/use-loader.rb', line 41

alias use_loader? use_loader

#use_multiline? (readonly)

Alias for #use_multiline.

[ GitHub ]

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

alias use_multiline? use_multiline

#use_readline (readonly)

Alias for #use_singleline.

[ GitHub ]

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

alias use_readline use_singleline

#use_readline? (readonly)

Alias for #use_singleline.

[ GitHub ]

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

alias use_readline? use_singleline

#use_reline (readonly)

Alias for #use_multiline.

[ GitHub ]

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

alias use_reline use_multiline

#use_reline? (readonly)

Alias for #use_multiline.

[ GitHub ]

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

alias use_reline? use_multiline

#use_singleline? (readonly)

Alias for #use_singleline.

[ GitHub ]

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

alias use_singleline? use_singleline

#use_tracer=(val) (writeonly)

[ GitHub ]

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

def use_tracer=(val)
  require_relative "ext/tracer" if val
  IRB.conf[:USE_TRACER] = val
end

#verbose (rw)

Whether verbose messages are displayed or not.

A copy of the default IRB.conf[:VERBOSE]

[ GitHub ]

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

attr_accessor :verbose

#verbose?Boolean (rw)

Returns whether messages are displayed or not.

[ GitHub ]

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

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 448

attr_accessor :with_debugger

#workspace_home (readonly)

The toplevel workspace, see #home_workspace

[ GitHub ]

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

attr_reader :workspace_home

Instance Method Details

#__inspect__

[ GitHub ]

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

alias __inspect__ inspect

#__to_s__

[ GitHub ]

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

alias __to_s__ to_s

#_set_last_value(value)

Alias for #set_last_value.

[ GitHub ]

  
# 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 177

private 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 198

private 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.message}" 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.

[ GitHub ]

  
# 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 658

def colorize_input(input, complete:)
  if IRB.conf[:USE_COLORIZE] && IRB::Color.colorable?
    lvars = local_variables || []
    if parse_command(input)
      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
[ GitHub ]

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

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
[ GitHub ]

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

attr_accessor :echo_on_assignment

#evaluate(statement, line_no)

This method is for internal use only.
[ GitHub ]

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

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)
  end

  nil
end

#evaluate_expression(code, line_no)

This method is for internal use only.
[ GitHub ]

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

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.

[ GitHub ]

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

[ GitHub ]

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

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.

[ GitHub ]

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

attr_accessor :ignore_sigint

#inspect (readonly) Also known as: #to_s

This method is for internal use only.
[ GitHub ]

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

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

This method is for internal use only.
[ GitHub ]

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

def inspect_last_value # :nodoc:
  @inspect_method.inspect_value(@last_value)
end

#local_variables

This method is for internal use only.
[ GitHub ]

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

def local_variables # :nodoc:
  workspace.binding.local_variables
end

#main

The top-level workspace, see WorkSpace#main

[ GitHub ]

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

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
[ GitHub ]

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

attr_accessor :newline_before_multiline_output

#parse_command(code)

[ GitHub ]

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

def parse_command(code)
  command_name, arg = code.strip.split(/\s+/, 2)
  return unless code.lines.size == 1 && command_name

  arg ||= ''
  command = command_name.to_sym
  # Command aliases are always command. example: $, @
  if (alias_name = command_aliases[command])
    return [alias_name, arg]
  end

  # Assignment-like expression is not a command
  return if arg.start_with?(ASSIGN_OPERATORS_REGEXP) && !arg.start_with?(/==|=~/)

  # Local variable have precedence over command
  return if local_variables.include?(command)

  # Check visibility
  public_method = !!Kernel.instance_method(:public_method).bind_call(main, command) rescue false
  private_method = !public_method && !!Kernel.instance_method(:method).bind_call(main, command) rescue false
  if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
    [command, arg]
  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.

[ GitHub ]

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

[ GitHub ]

  
# 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]

[ GitHub ]

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

attr_accessor :rc

#replace_workspace(workspace)

Replace the current workspace with the given #workspace.

[ GitHub ]

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

def replace_workspace(workspace)
  @workspace_stack.pop
  @workspace_stack.push(workspace)
end

#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 501.

[ GitHub ]

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

[ GitHub ]

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

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]

[ GitHub ]

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

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

[ GitHub ]

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

def use_loader
  IRB.conf[:USE_LOADER]
end

#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]

[ GitHub ]

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

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]

[ GitHub ]

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

attr_reader :use_singleline

#workspace

Workspace in the current context.

[ GitHub ]

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

def workspace
  @workspace_stack.last
end