DEV Community

Akshay Birajdar
Akshay Birajdar

Posted on • Updated on

Making Your Rails Console Interesting..

As a Rails developer, every time we work on a Rails project we heavily use rails console for development. I love the rails console as much as any Rails developer. It is the best place to experiment or validate your logic but...!

There are some of my personal nit-picks:

  • When we are playing around with multiple rails projects and end up opening rails console for multiple projects, then it becomes difficult to identify which rails console belong to which project.
  • Another problem with these plain rails console is identifying the rails environment in which we are. Until you run Rails.env you can't be sure.
  • But the biggest issue with the plain old rails console is; it is BORING! It's the same old screen every time I open.

Your rails console should be more declartive

Enter the Dotfiles

Time To spice things up

A little while ago I came to know about dotfiles by Rails conf talk by @bradurani. So basically the dotfiles are the configuration for Unix-y systems. In our daily dev life we use .bashrc, .bash_profile etc.
To my surprise ruby's IRB also has dotfile based configuration .irbrc and rails console is based on IRB so this config does apply to rails console too.

In this blog post, we will explore .irbrc prompt configuration and tackle the issues mentioned above.

IRB provides many prompt modes out of the box, these are NULL, DEFAULT, CLASSIC, SIMPLE, etc.
To switch between these prompt-modes, edit your .irbrc (located in HOME directory, if it's not present, create it.)

Simple IRB Prompt

There's a lot we need to understand before the above configuration starts making sense.

1 . Specific format for the irb prompt is used:

2 . Special strings are provided in irb as prompt-helpers:

Now, let's take a look at how IRB defines Classic mode & Simple mode.

Hurray!!, we completed the basic guide, now lets start tweaking with our .irbrc

Let's target our first issue, "How to inform the developer about the rails application name in the rails console?"

The first step, get the application name in rails.

We can find our rails app name in config/application.rb

Note: We will be using meetup rails app for examples



# config/application.rb

module Meetup
  class Application < Rails::Application
  ....
  end
end



Enter fullscreen mode Exit fullscreen mode

Notice that our application name is the module name for rails application class, now let's get it programmatically.

Now let's tackle the second issue, Rails environment information in rails console this is straightforward...

Now let's glue these together with .irbrc

Stitched irbrc

Oh! Wait we still haven't solved the important issue. Its still BOORING plain rails console.
Since irbrc is just ruby, your imagination is only the limitation for making it interesting. Following are my version of rails console's

  1. Colorful rails console using Rainbow gem or ANSI escape codes
    Color rails console
  2. Rails console with emojis
    Emoji

Thank you for reading!!

Top comments (5)

Collapse
 
jbutkowski profile image
Justin Butkowski

What font are you using for the emoji icons?

Collapse
 
thespectator profile image
Akshay Birajdar

I am using fonts pulled by oh-my-zsh & powerline.

Collapse
 
nickytonline profile image
Nick Taylor

Nice. I’m still a Ruby newb so this looks pretty cool. Also, congrats on your first post!

1st place in Mariokart

Collapse
 
thespectator profile image
Akshay Birajdar

Thank you! Nick.

Collapse
 
kiranmahale profile image
Kiran Mahale

Nice one. I was unknown about the dotfiles for IRB. Great!