Bird
Raised Fist0
Rubyprogramming~10 mins

IRB for interactive exploration in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - IRB for interactive exploration
Start IRB
↓
Prompt user input
↓
Evaluate input code
↓
Display result
↓
Wait for next input
Loop back to Prompt user input↓
Exit IRB on command
↩Loop back to Prompt user input
IRB starts and waits for your code input, runs it, shows the result, then waits again until you exit.
Execution Sample
Ruby
irb(main):001:0> 2 + 3
=> 5
irb(main):002:0> puts 'Hi!'
Hi!
=> nil
This shows typing code in IRB, it runs and shows the output immediately.
Execution Table
StepUser InputEvaluationOutputNext Action
12 + 3Calculate sum5Prompt next input
2puts 'Hi!'Print stringHi!Prompt next input
3exitExit commandSession endsStop IRB
💡 User types 'exit' to end the IRB session
Variable Tracker
VariableStartAfter Step 1After Step 2Final
_ (last result)nil5nilnil
Key Moments - 3 Insights
Why does IRB show '=> 5' after I type '2 + 3'?
IRB evaluates your expression and shows the result with '=>'. This is shown in execution_table step 1 under Output.
Why does 'puts' print output but return nil?
'puts' prints text to the screen but returns nil, so IRB shows 'Hi!' as output and '=> nil' as the return value, seen in execution_table step 2.
How do I stop IRB?
Typing 'exit' or pressing Ctrl+D ends the session, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 1?
Anil
B2 + 3
C5
DHi!
💡 Hint
Check the Output column in execution_table row 1
At which step does IRB stop running?
AStep 3
BStep 2
CStep 1
DNever stops
💡 Hint
Look at the Next Action column in execution_table
If you type 'puts "Hello"' in IRB, what will the return value be?
A"Hello"
Bnil
Cputs
DError
💡 Hint
See execution_table step 2 and variable_tracker for 'puts' behavior
Concept Snapshot
IRB lets you type Ruby code and see results instantly.
Type code, press Enter, see output with '=>'.
Use 'exit' to leave IRB.
Useful for quick testing and learning Ruby.
Full Transcript
IRB is an interactive Ruby shell. You start it and get a prompt. You type Ruby code, press Enter, and IRB runs it immediately. It shows the result with a '=>' sign. For example, typing '2 + 3' shows '=> 5'. If you use 'puts', it prints text but returns nil, so IRB shows the printed text and then '=> nil'. You keep typing commands and seeing results until you type 'exit' to stop IRB. This makes it easy to explore Ruby code step by step.

Practice

(1/5)
1. What is the main purpose of using irb in Ruby?
easy
A. To debug Ruby code with a graphical interface
B. To run Ruby code interactively and test snippets quickly
C. To create Ruby project files automatically
D. To compile Ruby code into an executable file

Solution

  1. Step 1: Understand what IRB stands for

    IRB means Interactive Ruby Shell, which lets you type and run Ruby code line by line.
  2. Step 2: Identify the main use of IRB

    It is mainly used to quickly test and explore Ruby code without creating files.
  3. Final Answer:

    To run Ruby code interactively and test snippets quickly -> Option B
  4. Quick Check:

    IRB = Interactive Ruby Shell for quick code testing [OK]
Hint: IRB runs code live, no files needed [OK]
Common Mistakes:
  • Thinking IRB compiles code
  • Confusing IRB with project setup tools
  • Assuming IRB has a graphical interface
2. Which command correctly starts the IRB session in your terminal?
easy
A. ruby irb
B. start irb
C. run ruby
D. irb

Solution

  1. Step 1: Recall the command to launch IRB

    The IRB session is started simply by typing irb in the terminal.
  2. Step 2: Check other options for correctness

    Commands like ruby irb, start irb, or run ruby are incorrect for starting IRB.
  3. Final Answer:

    irb -> Option D
  4. Quick Check:

    Type irb to start IRB [OK]
Hint: Just type 'irb' to start interactive Ruby [OK]
Common Mistakes:
  • Adding extra words like 'ruby' before irb
  • Using 'start' or 'run' commands incorrectly
  • Confusing IRB start with running Ruby files
3. What will be the output when you enter this in IRB?
5.times { |i| puts i }
medium
A. 5 times printed on one line
B. 1 2 3 4 5 (each on a new line)
C. 0 1 2 3 4 (each on a new line)
D. Error: undefined method 'times'

Solution

  1. Step 1: Understand the times method

    The times method runs the block 5 times, with i from 0 to 4.
  2. Step 2: Check what puts i does inside the block

    It prints each value of i on a new line, so output is 0,1,2,3,4 each on separate lines.
  3. Final Answer:

    0 1 2 3 4 (each on a new line) -> Option C
  4. Quick Check:

    5.times counts from 0 to 4 [OK]
Hint: times starts counting from zero [OK]
Common Mistakes:
  • Thinking times starts at 1
  • Expecting output on one line
  • Assuming times is undefined in IRB
4. You typed this in IRB:
puts 'Hello

What is the error and how do you fix it?
medium
A. SyntaxError due to missing closing quote; add a closing '
B. NameError because 'Hello' is undefined; define it first
C. No error; it prints Hello
D. RuntimeError because puts is not allowed in IRB

Solution

  1. Step 1: Identify the syntax problem

    The string started with a single quote but has no closing quote, causing a SyntaxError.
  2. Step 2: Fix the error by adding the missing quote

    Adding the closing single quote after Hello fixes the syntax and allows the code to run.
  3. Final Answer:

    SyntaxError due to missing closing quote; add a closing ' -> Option A
  4. Quick Check:

    Strings need matching quotes [OK]
Hint: Check for matching quotes in strings [OK]
Common Mistakes:
  • Ignoring missing quote errors
  • Thinking puts can't be used in IRB
  • Confusing SyntaxError with NameError
5. You want to quickly test a method in IRB that returns the square of a number. Which of these is the correct way to define and test it?
hard
A. def square(x); x * x; end; square(4)
B. def square x x * x end square 4
C. def square(x) { return x * x }; square(4)
D. def square(x): return x * x end; square(4)

Solution

  1. Step 1: Check correct Ruby method syntax

    Ruby methods use def name(params); body; end syntax. def square(x); x * x; end; square(4) follows this correctly.
  2. Step 2: Verify method call syntax

    Calling square(4) is correct. The other options have syntax errors or wrong Ruby style.
  3. Final Answer:

    def square(x); x * x; end; square(4) -> Option A
  4. Quick Check:

    Method def/end with parentheses is correct [OK]
Hint: Use def ... end with parentheses for methods [OK]
Common Mistakes:
  • Using braces {} for method body
  • Omitting parentheses incorrectly
  • Using Python-like syntax with colons
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard