Enhancing the Ruby IRB Console: Adding Custom Commands for Improved Workflow

How to add a clear command in Irb which I personally use most of the time.

To configure IRB (Interactive Ruby) to clear the terminal screen by typing clear instead of system("clear"), you can define a custom method that wraps the system("clear") command. Here’s an example of how you can achieve this:

  1. Create a .irbrc file in your home directory if it doesn’t already exist.
  2. Open the .irbrc file and add the following code:
   # .irbrc
 COLOR_RED = "\e[31m"
COLOR_GREEN = "\e[32m"
COLOR_YELLOW = "\e[33m"
COLOR_BLUE = "\e[34m"
COLOR_MAGENTA = "\e[35m"
COLOR_CYAN = "\e[36m"
COLOR_RESET = "\e[0m"

def lts


  current_time = Time.now
  hour = current_time.hour
  if hour >= 0 && hour < 12
    time_state="Good morning"
  elsif hour >= 12 && hour < 18
    time_state="Good Afternoon!"
    time_state="Good Evening!"
  puts " #{COLOR_BLUE}#{time_state}!#{COLOR_BLUE}#{COLOR_GREEN} #{auther}

This code defines a method named clear that calls system("clear") when invoked.

  1. Save the .irbrc file and restart your IRB session.

Now, when you’re using IRB, you can simply type clear and press Enter to clear the terminal screen.

Note: The behaviour of IRB can vary depending on your operating system and configuration. The method described above should work for most Unix-based systems, but it may not work as expected on Windows. If you’re using Windows, you can try using system("cls") instead of system("clear") in the .irbrc file to clear the terminal screen.

Leave a Comment

Your email address will not be published. Required fields are marked *