Bird
Raised Fist0
Rubyprogramming~20 mins

IRB customization in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
IRB Customization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
IRB Prompt Customization Output
What will be the output of the following IRB prompt customization code snippet when you start IRB?
Ruby
IRB.conf[:PROMPT][:MY_PROMPT] = {
  PROMPT_I: "myirb> ",
  PROMPT_S: "myirb%l> ",
  PROMPT_C: "myirb* ",
  PROMPT_N: "myirb? ",
  RETURN: "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :MY_PROMPT

# Then start IRB
AThe IRB prompt will show as 'myirb> ' for input lines.
BThe IRB prompt will show as the default 'irb(main):001:0> '.
CThe IRB prompt will show as '>> ' for input lines.
DThe IRB prompt will be empty with no visible prompt.
Attempts:
2 left
💡 Hint
Look at the PROMPT_I key in the custom prompt hash and the PROMPT_MODE setting.
🧠 Conceptual
intermediate
1:30remaining
Purpose of IRB.conf[:SAVE_HISTORY]
What is the purpose of setting IRB.conf[:SAVE_HISTORY] = 1000 in IRB customization?
AIt changes the prompt to show the last 1000 commands.
BIt disables saving any command history in IRB.
CIt sets the maximum number of concurrent IRB sessions to 1000.
DIt limits the number of lines saved in the IRB command history to 1000.
Attempts:
2 left
💡 Hint
Think about what 'SAVE_HISTORY' might control in a command line tool.
🔧 Debug
advanced
2:30remaining
Fixing IRB Custom Prompt Syntax Error
This IRB customization code causes a syntax error. Which option fixes it correctly?
Ruby
IRB.conf[:PROMPT][:CUSTOM] = {
  PROMPT_I: '>> ',
  PROMPT_S: '>> ',
  PROMPT_C: '>> ',
  PROMPT_N: '>> ',
  RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :CUSTOM
AAdd a comma after the PROMPT_N line to separate the hash entries.
BReplace all single quotes with double quotes.
CRemove the RETURN key from the hash.
DChange the hash braces {} to square brackets [].
Attempts:
2 left
💡 Hint
Look carefully at the commas between hash entries in Ruby syntax.
❓ Predict Output
advanced
2:00remaining
IRB History File Location
Given this IRB configuration snippet, what will be the path of the history file used by IRB?
Ruby
IRB.conf[:HISTORY_FILE] = File.expand_path('~/.my_irb_history')
AThe history file will be saved as 'history.txt' in the home directory.
BThe history file will be saved in the current working directory as '.my_irb_history'.
CThe history file will be saved as '.my_irb_history' in the user's home directory.
DIRB will not save any history file with this setting.
Attempts:
2 left
💡 Hint
File.expand_path with '~' expands to the user's home directory.
🚀 Application
expert
3:00remaining
Custom IRB Prompt with Dynamic Time
Which option correctly defines a custom IRB prompt that shows the current time before the prompt symbol?
A
IRB.conf[:PROMPT][:TIME_PROMPT] = {
  PROMPT_I: proc { "[Time.now] irb> " },
  PROMPT_S: 'irb%l> ',
  PROMPT_C: 'irb* ',
  PROMPT_N: 'irb? ',
  RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :TIME_PROMPT
B
IRB.conf[:PROMPT][:TIME_PROMPT] = {
  PROMPT_I: -> { "[#{Time.now.strftime('%H:%M:%S')}] irb> " },
  PROMPT_S: 'irb%l> ',
  PROMPT_C: 'irb* ',
  PROMPT_N: 'irb? ',
  RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :TIME_PROMPT
C
IRB.conf[:PROMPT][:TIME_PROMPT] = {
  PROMPT_I: "[#{Time.now.strftime('%H:%M:%S')}] irb> ",
  PROMPT_S: 'irb%l> ',
  PROMPT_C: 'irb* ',
  PROMPT_N: 'irb? ',
  RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :TIME_PROMPT
D
IRB.conf[:PROMPT][:TIME_PROMPT] = {
  PROMPT_I: '[Time.now] irb> ',
  PROMPT_S: 'irb%l> ',
  PROMPT_C: 'irb* ',
  PROMPT_N: 'irb? ',
  RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :TIME_PROMPT
Attempts:
2 left
💡 Hint
The prompt string must be a callable (like a lambda) to update dynamically each time.

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

  1. Step 1: Understand the role of ~/.irbrc

    The ~/.irbrc file is used to customize the IRB shell environment.
  2. Step 2: Identify customization effects

    Customizations include changing the prompt, loading libraries, and saving history, which affect how IRB looks and works.
  3. Final Answer:

    To change how the IRB shell looks and behaves -> Option C
  4. Quick Check:

    IRB customization = change shell behavior [OK]
Hint: Remember ~/.irbrc customizes IRB shell behavior [OK]
Common Mistakes:
  • Thinking it installs gems automatically
  • Confusing with Ruby code compilation
  • Assuming it creates Ruby classes
2. Which of the following is the correct way to load the 'date' library automatically when IRB starts by editing ~/.irbrc?
easy
A. require 'date'
B. load 'date'
C. import 'date'
D. include 'date'

Solution

  1. Step 1: Recall Ruby syntax for loading libraries

    Ruby uses require 'library_name' to load external libraries.
  2. Step 2: Check each option

    require 'date' is correct; load expects a file path, import and include are incorrect here.
  3. Final Answer:

    require 'date' -> Option A
  4. Quick Check:

    Load library = require 'library' [OK]
Hint: Use require to load libraries in ~/.irbrc [OK]
Common Mistakes:
  • Using import which is not Ruby syntax
  • Using include which mixes modules, not load libs
  • Using load without full file path
3. Given this ~/.irbrc snippet:
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:SAVE_HISTORY] = 1000

What effect does this have when you start IRB?
medium
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

  1. Step 1: Understand IRB.conf[:PROMPT_MODE]

    Setting :PROMPT_MODE to :SIMPLE changes IRB prompt to a simple style.
  2. Step 2: Understand IRB.conf[:SAVE_HISTORY]

    Setting :SAVE_HISTORY to 1000 means IRB saves the last 1000 commands in history.
  3. Final Answer:

    Sets a simple prompt and saves last 1000 commands in history -> Option D
  4. 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

  1. Step 1: Check the data type of the value

    :SAVE_HISTORY expects an integer number, not a string.
  2. Step 2: Identify the error cause

    Using a string like '1000' causes IRB to ignore the setting, so history is not saved.
  3. Final Answer:

    The value should be an integer, not a string -> Option B
  4. 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?
hard
A. require 'json' IRB.conf[:PROMPT][:CUSTOM] = { PROMPT_I: "[#{Time.now.strftime('%H:%M:%S')}] > " } IRB.conf[:PROMPT_MODE] = :CUSTOM IRB.conf[:SAVE_HISTORY] = 500
B. require 'json' IRB.conf[:PROMPT_MODE] = :SIMPLE IRB.conf[:SAVE_HISTORY] = '500'
C. load 'json' IRB.conf[:PROMPT_MODE] = :DEFAULT IRB.conf[:SAVE_HISTORY] = 500
D. require 'json' IRB.conf[:PROMPT_MODE] = :CUSTOM IRB.conf[:SAVE_HISTORY] = 1000

Solution

  1. Step 1: Load the 'json' library correctly

    Use require 'json' to load the library automatically.
  2. Step 2: Define a custom prompt with current time

    Set IRB.conf[:PROMPT][:CUSTOM] with a prompt string showing current time using Ruby's Time.now.strftime.
  3. Step 3: Set prompt mode and save history

    Assign IRB.conf[:PROMPT_MODE] = :CUSTOM to use the custom prompt and set IRB.conf[:SAVE_HISTORY] = 500 as an integer to save 500 commands.
  4. Final Answer:

    require 'json', custom prompt with current time, :CUSTOM mode, SAVE_HISTORY = 500 -> Option A
  5. Quick Check:

    Require json, custom prompt with time, save 500 history [OK]
Hint: Combine require, custom prompt hash, and integer history save [OK]
Common Mistakes:
  • Using string instead of integer for history
  • Not defining custom prompt hash properly
  • Using load instead of require for libraries
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