Overview
Reading the code of irb.
Preparation
- Clone the irb source code
git clone git@github.com:ruby/irb.git
- Modify the entry point Since the original state runs the irb located in the Ruby installation directory, we will run the local irb instead.
#!/usr/bin/env ruby
#
# irb.rb - interactive ruby
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__)) # Add this line
require "irb"
IRB.start(__FILE__)
- Debug at any point
Code Reading
- Executing the irb command
- ruby/irb/blob/master/exe/irb#L9
- Executes
IRB.start(__FILE__)with the filename of the executing file as an argument.
- Executes
- Setup process at startup
- Starting irb
- ruby/irb/blob/master/lib/irb.rb#L1001
- Loads the history
- History is the execution history of commands. It is written to a file (~/.irb_history).
- Loads the history
- Executing input values
- ruby/irb/blob/master/lib/irb.rb#L1041
- ruby/irb/blob/master/lib/irb/context.rb#L589
- Processes are branched and executed based on the input values.
- For expressions:
- ruby/irb/blob/master/lib/irb/context.rb#L589
- ruby/irb/blob/master/lib/irb/context.rb#L609
- ruby/irb/blob/master/lib/irb/workspace.rb#L120
- Evaluates the expression with
eval.
- Evaluates the expression with
- For commands:
- ruby/irb/blob/master/lib/irb/command/base.rb#L55
- It was unclear what the command was, but it does not seem to be a feature for users.
- ruby/irb/blob/master/lib/irb/command/base.rb#L55