0
0
Rubyprogramming~5 mins

IRB customization in Ruby

Choose your learning style9 modes available
Introduction

IRB customization lets you change how the Ruby interactive shell works to make coding easier and faster for you.

You want to add helpful commands or shortcuts to your Ruby shell.
You want to change the colors or prompt style to see things more clearly.
You want to automatically load your favorite libraries every time you start IRB.
You want to save time by setting up your environment just how you like it.
You want to improve your coding experience with extra features in IRB.
Syntax
Ruby
# Create or edit ~/.irbrc file
# Example content:
require 'irb/ext/save-history'

IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "~/.irb-history"

# Customize prompt
IRB.conf[:PROMPT_MODE] = :SIMPLE

The ~/.irbrc file is where you put your IRB custom settings.

You write normal Ruby code in this file to change IRB behavior.

Examples
This makes the Date class ready to use every time you open IRB.
Ruby
# Load a library automatically
require 'date'
This sets a simple prompt like irb> instead of the default one.
Ruby
# Change IRB prompt style
IRB.conf[:PROMPT_MODE] = :SIMPLE
This saves your last 1000 commands so you can recall them later.
Ruby
# Save command history
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "~/.irb-history"
Sample Program

This example sets up IRB to save 500 commands in history, uses a simple prompt, and prints a welcome message when IRB starts.

Ruby
# ~/.irbrc example
require 'irb/ext/save-history'

IRB.conf[:SAVE_HISTORY] = 500
IRB.conf[:HISTORY_FILE] = "~/.irb-history"
IRB.conf[:PROMPT_MODE] = :SIMPLE

puts "Welcome to your customized IRB!"
OutputSuccess
Important Notes

Remember to restart IRB after changing ~/.irbrc to see your changes.

You can add any Ruby code in ~/.irbrc, so be careful with errors.

Customizing IRB can make your coding faster and more fun!

Summary

IRB customization lets you change how the Ruby shell looks and works.

You do this by editing the ~/.irbrc file with Ruby code.

Common changes include loading libraries, saving history, and changing the prompt.