0
0
Rubyprogramming~20 mins

IRB customization in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 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.