IRB customization in Ruby - Time & Space Complexity
When customizing IRB, we often run code that sets up the environment. Understanding how the time to apply these customizations grows helps us keep the setup fast.
We want to know how the time to load and apply custom settings changes as the customization code grows.
Analyze the time complexity of this IRB customization snippet.
require 'irb'
IRB.conf[:PROMPT][:MY_PROMPT] = {
PROMPT_I: '>> ',
PROMPT_S: '>> ',
PROMPT_C: '?> ',
PROMPT_N: '>> ',
RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :MY_PROMPT
IRB.start
This code sets a custom prompt style for IRB and then starts the IRB session with that style.
Look for repeated actions that affect time.
- Primary operation: Assigning prompt settings in a hash and starting IRB.
- How many times: Each assignment happens once; IRB.start runs the interactive session once.
As the number of customization lines grows, the time to apply them grows roughly in a straight line.
| Input Size (lines of customization) | Approx. Operations |
|---|---|
| 10 | 10 assignments + 1 IRB start |
| 100 | 100 assignments + 1 IRB start |
| 1000 | 1000 assignments + 1 IRB start |
Pattern observation: The time grows linearly with the number of customization lines.
Time Complexity: O(n)
This means the time to apply customizations grows directly with how many lines of setup code you have.
[X] Wrong: "Adding more customization lines won't affect startup time much because it's just configuration."
[OK] Correct: Each line runs code that takes time, so more lines mean more time to process before IRB starts.
Knowing how setup time grows helps you write efficient startup scripts and shows you understand how code size affects performance.
"What if we changed the customization to load settings from a file instead of inline code? How would the time complexity change?"
Practice
~/.irbrc file in Ruby's IRB?Solution
Step 1: Understand the role of
The~/.irbrc~/.irbrcfile 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 CQuick Check:
IRB customization = change shell behavior [OK]
~/.irbrc customizes IRB shell behavior [OK]- Thinking it installs gems automatically
- Confusing with Ruby code compilation
- Assuming it creates Ruby classes
~/.irbrc?Solution
Step 1: Recall Ruby syntax for loading libraries
Ruby usesrequire 'library_name'to load external libraries.Step 2: Check each option
require 'date'is correct;loadexpects a file path,importandincludeare incorrect here.Final Answer:
require 'date' -> Option AQuick Check:
Load library = require 'library' [OK]
require to load libraries in ~/.irbrc [OK]- Using
importwhich is not Ruby syntax - Using
includewhich mixes modules, not load libs - Using
loadwithout full file path
~/.irbrc snippet:IRB.conf[:PROMPT_MODE] = :SIMPLE IRB.conf[:SAVE_HISTORY] = 1000
What effect does this have when you start IRB?
Solution
Step 1: Understand
SettingIRB.conf[:PROMPT_MODE]:PROMPT_MODEto:SIMPLEchanges IRB prompt to a simple style.Step 2: Understand
SettingIRB.conf[:SAVE_HISTORY]:SAVE_HISTORYto 1000 means IRB saves the last 1000 commands in history.Final Answer:
Sets a simple prompt and saves last 1000 commands in history -> Option DQuick Check:
Prompt = simple, History saved = 1000 [OK]
- Thinking it disables history
- Assuming syntax error from valid config
- Confusing prompt style with color only
~/.irbrc file:IRB.conf[:SAVE_HISTORY] = '1000'
But IRB does not save history as expected. What is the problem?
Solution
Step 1: Check the data type of the value
:SAVE_HISTORYexpects 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 BQuick Check:
Integer value needed for history count [OK]
- Using quotes around numbers
- Thinking IRB can't save history
- Believing a full restart is needed
~/.irbrc snippet correctly does this?Solution
Step 1: Load the 'json' library correctly
Userequire 'json'to load the library automatically.Step 2: Define a custom prompt with current time
SetIRB.conf[:PROMPT][:CUSTOM]with a prompt string showing current time using Ruby'sTime.now.strftime.Step 3: Set prompt mode and save history
AssignIRB.conf[:PROMPT_MODE] = :CUSTOMto use the custom prompt and setIRB.conf[:SAVE_HISTORY] = 500as an integer to save 500 commands.Final Answer:
require 'json', custom prompt with current time, :CUSTOM mode, SAVE_HISTORY = 500 -> Option AQuick Check:
Require json, custom prompt with time, save 500 history [OK]
- Using string instead of integer for history
- Not defining custom prompt hash properly
- Using load instead of require for libraries
Ready to prove it?
Pass the test and this pattern moves unknown
