# frozen_string_literal: false
# irb/init.rb - irb initialize module
# $Release Version: 0.9.6$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
def IRB.setup(ap_path, argv: ::ARGV)
IRB.parse_opts(argv: argv)
unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]]
fail UndefinedPromptMode, @CONF[:PROMPT_MODE]
def IRB.init_config(ap_path)
# class instance variables
@TRACER_INITIALIZED = false
unless ap_path and @CONF[:AP_NAME]
ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")
@CONF[:AP_NAME] = File::basename(ap_path, ".rb")
@CONF[:IRB_LIB_PATH] = File.dirname(__FILE__)
@CONF[:LOAD_MODULES] = []
@CONF[:USE_SINGLELINE] = false unless defined?(ReadlineInputMethod)
@CONF[:USE_COLORIZE] = true
@CONF[:INSPECT_MODE] = true
@CONF[:USE_TRACER] = false
@CONF[:USE_LOADER] = false
@CONF[:IGNORE_SIGINT] = true
@CONF[:IGNORE_EOF] = false
@CONF[:ECHO_ON_ASSIGNMENT] = nil
@CONF[:OMIT_ON_ASSIGNMENT] = nil
@CONF[:EVAL_HISTORY] = nil
@CONF[:SAVE_HISTORY] = 1000
@CONF[:BACK_TRACE_LIMIT] = 16
:PROMPT_I => "%N(%m):%03n:%i> ",
:PROMPT_N => "%N(%m):%03n:%i> ",
:PROMPT_S => "%N(%m):%03n:%i%l ",
:PROMPT_C => "%N(%m):%03n:%i* ",
:PROMPT_I => "%N(%m):%03n:%i> ",
:PROMPT_N => "%N(%m):%03n:%i> ",
:PROMPT_S => "%N(%m):%03n:%i%l ",
:PROMPT_C => "%N(%m):%03n:%i* ",
:PROMPT_I => "%N(%m):%03n:%i> ",
@CONF[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL)
@CONF[:AUTO_INDENT] = true
@CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
@CONF[:SINGLE_IRB] = false
@CONF[:LC_MESSAGES] = Locale.new
@CONF[:LC_MESSAGES].load("irb/error.rb")
def IRB.parse_opts(argv: ::ARGV)
@CONF[:LOAD_MODULES].push opt if opt
load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
set_encoding("UTF-8", "UTF-8")
when /^-E(.+)?/, /^--encoding(?:=(.+))?/
set_encoding(*opt.split(':', 2))
@CONF[:INSPECT_MODE] = argv.shift
@CONF[:INSPECT_MODE] = true
@CONF[:INSPECT_MODE] = false
when "--singleline", "--readline", "--legacy"
@CONF[:USE_SINGLELINE] = true
when "--nosingleline", "--noreadline"
@CONF[:USE_SINGLELINE] = false
when "--multiline", "--reidline"
@CONF[:USE_MULTILINE] = true
when "--nomultiline", "--noreidline"
@CONF[:USE_MULTILINE] = false
when "--echo-on-assignment"
@CONF[:ECHO_ON_ASSIGNMENT] = true
when "--noecho-on-assignment"
@CONF[:ECHO_ON_ASSIGNMENT] = false
when "--omit-on-assignment"
@CONF[:OMIT_ON_ASSIGNMENT] = true
when "--noomit-on-assignment"
@CONF[:OMIT_ON_ASSIGNMENT] = false
@CONF[:USE_COLORIZE] = true
@CONF[:USE_COLORIZE] = false
when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
prompt_mode = opt.upcase.tr("-", "_").intern
@CONF[:PROMPT_MODE] = prompt_mode
@CONF[:PROMPT_MODE] = :NULL
@CONF[:PROMPT_MODE] = :INF_RUBY
when "--sample-book-mode", "--simple-prompt"
@CONF[:PROMPT_MODE] = :SIMPLE
@CONF[:USE_TRACER] = true
when /^--back-trace-limit(?:=(.+))?/
@CONF[:BACK_TRACE_LIMIT] = ($1 || argv.shift).to_i
when /^--context-mode(?:=(.+))?/
@CONF[:CONTEXT_MODE] = ($1 || argv.shift).to_i
@CONF[:SINGLE_IRB] = true
fail UnrecognizedSwitch, opt
load_path.collect! do |path|
/\A\.\// =~ path ? path : File.expand_path(path)
$LOAD_PATH.unshift(*load_path)
rescue LoadError, Errno::ENOENT
rescue # StandardError, ScriptError
print "load error: #{rc_file}\n"
print $!.class, ": ", $!, "\n"
for err in $@[0, $@.size - 2]
def IRB.rc_file(ext = IRBRC_EXT)
if !@CONF[:RC_NAME_GENERATOR]
rc_file_generators do |rcgen|
@CONF[:RC_NAME_GENERATOR] ||= rcgen
if File.exist?(rcgen.call(IRBRC_EXT))
@CONF[:RC_NAME_GENERATOR] = rcgen
case rc_file = @CONF[:RC_NAME_GENERATOR].call(ext)
fail IllegalRCNameGenerator
# enumerate possible rc-file base name generators
def IRB.rc_file_generators
yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
if xdg_config_home = ENV["XDG_CONFIG_HOME"]
irb_home = File.join(xdg_config_home, "irb")
unless File.exist? irb_home
FileUtils.mkdir_p irb_home
yield proc{|rc| irb_home + "/irb#{rc}"}
yield proc{|rc| home+"/.irb#{rc}"}
yield proc{|rc| current_dir+"/.config/irb/irb#{rc}"}
yield proc{|rc| current_dir+"/.irb#{rc}"}
yield proc{|rc| current_dir+"/irb#{rc.sub(/\A_?/, '.')}"}
yield proc{|rc| current_dir+"/_irb#{rc}"}
yield proc{|rc| current_dir+"/$irb#{rc}"}
for m in @CONF[:LOAD_MODULES]
warn "#{err.class}: #{err}", uplevel: 0
DefaultEncodings = Struct.new(:external, :internal)
def set_encoding(extern, intern = nil, override: true)
verbose, $VERBOSE = $VERBOSE, nil
Encoding.default_external = extern unless extern.nil? || extern.empty?
Encoding.default_internal = intern unless intern.nil? || intern.empty?
[$stdin, $stdout, $stderr].each do |io|
io.set_encoding(extern, intern)
@CONF[:LC_MESSAGES].instance_variable_set(:@override_encoding, extern)
@CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern)