0
0
Rubyprogramming~5 mins

IRB for interactive exploration in Ruby

Choose your learning style9 modes available
Introduction

IRB lets you try Ruby code right away. It helps you learn and test ideas quickly without writing a full program.

You want to check how a Ruby method works.
You need to test a small piece of code before adding it to your program.
You want to explore Ruby commands and see results immediately.
You are learning Ruby and want to practice coding interactively.
Syntax
Ruby
irb

Type irb in your terminal to start the interactive Ruby shell.

Use exit or quit to leave IRB.

Examples
This shows starting IRB, doing simple math, printing text, and exiting.
Ruby
irb
> 2 + 3
=> 5
> puts "Hello"
Hello
=> nil
> exit
Assign a variable and use it inside a string with interpolation.
Ruby
irb
> name = "Alice"
> puts "Hello, #{name}!"
Hello, Alice!
=> nil
> exit
Sample Program

This example uses IRB to print numbers from 1 to 10 using a loop.

Ruby
irb
> 10.times { |i| puts "Number: #{i + 1}" }
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10
=> 10
> exit
OutputSuccess
Important Notes

IRB is great for quick tests but not for full programs.

You can use arrow keys to navigate your command history in IRB.

IRB shows the result of each expression automatically.

Summary

IRB is a tool to run Ruby code interactively.

It helps you learn and test code quickly.

Start it by typing irb in your terminal.