Making Your Rails Console Interesting..
Tags: rails, ruby, dotfiles
Published: 2020-01-15 09:47:49

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
<h2> <center> Enter the Dotfiles </center></h2>
![Time To spice things up](https://thepracticaldev.s3.amazonaws.com/i/fjsok0yoffci13t3kn89.jpg)

A little while ago I came to know about dotfiles by [Rails conf talk](https://www.youtube.com/watch?v=V0p7pWSxOXw) by [@bradurani](https://twitter.com/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.)

{% gist https://gist.github.com/the-spectator/fd599bf66f5b60e974cffe9665c1e6c8 %}

![Simple IRB Prompt](https://thepracticaldev.s3.amazonaws.com/i/4rq4isqm4q3c259mtp5l.gif)

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

1 . Specific format for the irb prompt is used:

{% gist https://gist.github.com/the-spectator/90bb6ca7380dfc366f936bfe27ce6f01 %}

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

{% gist https://gist.github.com/the-spectator/5ca4ffb2742d393fa83a39f9e69ec509 %}

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

{% gist https://gist.github.com/the-spectator/c0ee4076402c5f5ebc4ef49ce75f5e7c %}

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

```ruby
# config/application.rb

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

```

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

{% gist https://gist.github.com/the-spectator/ba03810f168774573dd0e29eb9384d58 %}

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

{% gist https://gist.github.com/the-spectator/e4d711df717d562f4b3a9ee7315ddf8a %}

Now let's glue these together with .irbrc

{% gist https://gist.github.com/the-spectator/87e23c31e1a4ed73c6b9344af190c5f1 %}

![Stitched irbrc](https://thepracticaldev.s3.amazonaws.com/i/b5puyceh6qqp0wdx9axp.gif)

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
{% gist https://gist.github.com/the-spectator/b2610d0faad036ff12438a1d71b346c7 %}
![Color rails console](https://thepracticaldev.s3.amazonaws.com/i/mx556wm981muglmz6wpd.gif)
2. Rails console with emojis
{% gist https://gist.github.com/the-spectator/a86ccd994bd8e56f40fb1dd7b252319e %}
![Emoji](https://thepracticaldev.s3.amazonaws.com/i/8ulj03ohvfuv3kjdn1ca.gif)

Thank you for reading!!