Ruby

How to keep your irb session prompt and output shorter

While experimenting with code snippets on your irb (Interactive Ruby Interpreter) session, you might want to keep things clearer. This minipost demonstrates how to hide session returns and make irb prompt shorter allowing better focus on code execution.

You can start your irb session by typing the command irb on your console. After that, irb session starts by printing out its prompt. Depending on the version of Ruby installed in the system and some settings, irb session prompt differs across different installations. A typical example is the following:

$ irb
irb(main):001:0> puts "Hello world"
Hello world
=> nil

Using the irb options/flags

irb can become very handly as it supports couple of options/flags that can alter its appearance. The first flag is the --simple-prompt that keeps irb ‘s outputted prompt very short. Literally, the prompt becomes just two angle brackets >>. The second option is the --noecho flag which suppresses the return value of any executed expression. One very important thing is that we can start the irb by combining both of the flags in a single irb session, like the following:

$ irb --simple-prompt --noecho
>> puts "Hello world"
Hello world

With the use of those two flags irb session output and prompt is shorter allowing to focus only at the code expression you execute in the session.

Add the flags to .irbrc file

Rather than having to type the above flags every time you need to run your irb session, you can add the flags functionality to your .irbrc file with a slightly different syntax but with the exact same functionality. Therefore, open your .irbrc file:

$ sudo nano ~/.irbrc

and paste the following:

IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:PROMPT][:SIMPLE][:RETURN] = ""

Then restart your irb session and you will get the same result as starting the irb session using the --simple-prompt --noecho flags.  Following this minipost all the code samples in this blog that run on an irb session follow the --simple-prompt --noecho notation unless it is specified otherwise.

Additional information:

Buy Me A Coffee

Read also the following