Using IRB: A Guide to Exploring Ruby Code in an Interactive Environment
Do you want to write code in Ruby and test it instantly without creating a file? In this guide, you will learn step by step how to run Ruby codes, test multi-line blocks, and customize your session using the IRB (Interactive Ruby) tool.
🧠 What Will You Learn in This Guide?
- Starting and ending IRB
- Executing code and interpreting return values
- Installing external libraries and local Ruby files
- Customize IRB session with file
.irbrc - Frequently used IRB commands and shortcuts
🔹 1. What is IRB?
IRB (Interactive Ruby) is Ruby's interactive command line tool. It works as a REPL (Read–Eval–Print–Loop):
- Reads the command
- Evaluates
- Prints the result
- Repeats the cycle
With IRB, you can test small pieces of code, try out methods, or quickly observe library behavior.
🔹 2. Starting and Stopping IRB
If Ruby is installed on your system, IRB comes automatically as well. To start IRB, type this in the terminal:
irb
At the prompt you will see something like this:
irb(main):001:0>
This means that IRB is working and you can now write Ruby codes. To exit, you can type exit or use the keys CTRL + D.
🔹 3. Code Execution and Return Value
Every expression you write in IRB produces a return value. This value is indicated by the sign =>.
2 + 2
# => 4
You can output it to the screen with the puts method, but it returns nil:
puts "Merhaba Dünya"
# Merhaba Dünya
# => nil
💡 Difference between output and return value:
- Output is the value printed to the screen by methods such as
putsorprint. - The return value is the result of the Ruby expression and is denoted by
=>.
🔹 4. Variables and Multi-Line Codes
You can define variables in IRB just like in Ruby:
dogum_yili = 1995
vefat_yili = 2050
yas = vefat_yili - dogum_yili
# => 55
When you write multi-line codes (for example, do...end or class blocks), IRB waits for the code to finish. In this way, the blocks will not work until they are completed.
["Elma", "Armut", "Ayva"].select do |meyve|
meyve.include?("A")
end
# => ["Armut", "Ayva"]
🔹 5. Using External Libraries (Gem)
You can use Ruby's standard libraries or the gems you have installed in the IRB.
require 'net/http'
# => true
To make an HTTP request:
uri = URI.parse("http://icanhazip.com")
response = Net::HTTP.get_response(uri)
puts response.body
If gem is not installed you will get this error:
require 'httparty'
# LoadError: cannot load such file -- httparty
💡 In this case, log out and install with this command:
gem install httparty
🔹 6. Include Your Local Code in the IRB
You can use the -r parameter to test your own Ruby files in the IRB:
irb -r ./ip_yakalayici
Then you can call your class directly:
yakalayici = IPYakalayici.new
yakalayici.get_ip
# => 203.0.113.52
💡 Note: The require command automatically detects the .rb extension, so you do not need to specify it. This method allows you to quickly test your own classes and methods.
🔹 7. Customizing IRB Session (.irbrc)
To customize the IRB, create a file named .irbrc in your home directory:
nano ~/.irbrc
You can add the following lines:
AutoComplete:
require 'irb/completion'
Command History:
IRB.conf[:SAVE_HISTORY] = 1000
Auto-Indenting:
IRB.conf[:AUTO_INDENT] = true
Helper Method:
def gecmis(satir=10)
puts Readline::HISTORY.to_a.last(satir)
end
This method lists your last 10 commands.
💡 Each additional library slightly increases the IRB boot time. It is more efficient to load the ones you do not use frequently manually with require.
💬 Frequently Asked Questions (FAQ)
1️⃣ What is IRB? How is it different from Ruby? IRB is Ruby's interactive environment. It is useful for trying code without saving a file.
2️⃣ What is the difference between output and return value? Screen output is printed by commands like puts, while the return value is the result of the Ruby expression (denoted by =>).
3️⃣ Why does IRB open slowly? If there is more require in your .irbrc file, the opening time will be longer. You can remove unnecessary modules.
4️⃣ Why don't we specify the .rb extension? Ruby automatically detects the .rb extension in the require command.
5️⃣ Is IRB safe? Yes, but you should not add code you do not trust to your .irbrc file.
🎯 Result
IRB is an indispensable testing and discovery tool for Ruby developers. Ideal for trying out small pieces of code, testing gems, or analyzing methods. You can increase your productivity by customizing it with the .irbrc file.
Using this interactive environment, you can start your Ruby projects quickly and safely on the GenixNode platform.

