📖 Scenario: You often use IRB (Interactive Ruby) to test small Ruby code snippets quickly. Customizing IRB can make your work easier and more fun by adding colors, changing prompts, or loading helpful methods automatically.
🎯 Goal: You will create a simple IRB configuration file that changes the prompt and adds a helper method to greet the user.
📋 What You'll Learn
Create a Ruby hash called IRB.conf with a nested hash for :PROMPT containing a custom prompt named :MY_PROMPT
Set the :MY_PROMPT prompt to show "MyIRB> " as the prompt string
Add a helper method called greet that takes a name parameter and returns "Hello, <name>!"
Print the result of calling greet("Friend") to show the greeting
💡 Why This Matters
🌍 Real World
Customizing IRB helps Ruby developers work faster and more comfortably by tailoring the interactive console to their preferences.
💼 Career
Many Ruby developers customize IRB or similar REPLs to improve productivity, making this skill useful for software development roles.
Progress0 / 4 steps
1
Create the IRB prompt configuration
Create a Ruby hash called IRB.conf with a nested hash for :PROMPT containing a custom prompt named :MY_PROMPT that has a key :PROMPT_I set to the string "MyIRB> ".
Ruby
Hint
Use a nested hash structure with keys :PROMPT and :MY_PROMPT. Set :PROMPT_I to "MyIRB> "
2
Add a helper method called greet
Define a method called greet that takes one parameter called name and returns the string "Hello, <name>!" where <name> is replaced by the parameter value.
Ruby
Hint
Use string interpolation with #{name} inside double quotes to create the greeting.
3
Call the greet method with "Friend"
Call the method greet with the argument "Friend" and assign the result to a variable called message.
Ruby
Hint
Assign the result of greet("Friend") to message.
4
Print the greeting message
Print the variable message to display the greeting.
Ruby
Hint
Use puts message to print the greeting.
Practice
(1/5)
1. What is the main purpose of customizing the ~/.irbrc file in Ruby's IRB?
easy
A. To create new Ruby classes
B. To install new Ruby gems automatically
C. To change how the IRB shell looks and behaves
D. To compile Ruby code faster
Solution
Step 1: Understand the role of ~/.irbrc
The ~/.irbrc file is used to customize the IRB shell environment.
Step 2: Identify customization effects
Customizations include changing the prompt, loading libraries, and saving history, which affect how IRB looks and works.
Final Answer:
To change how the IRB shell looks and behaves -> Option C
A. Disables command history and uses default prompt
B. Changes prompt color but does not save history
C. Causes a syntax error and IRB won't start
D. Sets a simple prompt and saves last 1000 commands in history
Solution
Step 1: Understand IRB.conf[:PROMPT_MODE]
Setting :PROMPT_MODE to :SIMPLE changes IRB prompt to a simple style.
Step 2: Understand IRB.conf[:SAVE_HISTORY]
Setting :SAVE_HISTORY to 1000 means IRB saves the last 1000 commands in history.
Final Answer:
Sets a simple prompt and saves last 1000 commands in history -> Option D
Quick Check:
Prompt = simple, History saved = 1000 [OK]
Hint: Look for keys in IRB.conf to know prompt and history settings [OK]
Common Mistakes:
Thinking it disables history
Assuming syntax error from valid config
Confusing prompt style with color only
4. You wrote this in your ~/.irbrc file:
IRB.conf[:SAVE_HISTORY] = '1000'
But IRB does not save history as expected. What is the problem?
medium
A. The key :SAVE_HISTORY is invalid
B. The value should be an integer, not a string
C. IRB does not support saving history
D. You must restart the computer for changes to apply
Solution
Step 1: Check the data type of the value
:SAVE_HISTORY expects an integer number, not a string.
Step 2: Identify the error cause
Using a string like '1000' causes IRB to ignore the setting, so history is not saved.
Final Answer:
The value should be an integer, not a string -> Option B
Quick Check:
Integer value needed for history count [OK]
Hint: Use numbers, not strings, for numeric IRB config values [OK]
Common Mistakes:
Using quotes around numbers
Thinking IRB can't save history
Believing a full restart is needed
5. You want to customize IRB so that it automatically requires the 'json' library, sets a custom prompt showing the current time, and saves 500 commands in history. Which ~/.irbrc snippet correctly does this?