johnmurray.io /log

/Per-Project Console

If this isn't new to you, then oh well. Maybe it's a common practice and I'm just out of the loop. Either way, it's so useful! (I <3 interactive consoles)

Pry is a wonderful interactive environment for Ruby. It is (more or less) a replacement to IRB. We already know that interactive environments are highly useful and the Rails Console is a great example of utilizing that interactive environment effectively.

However, if you're like me, then you don't work in Rails. The majority of my work is in custom Ruby programs, EM-driven programs and Sinatra APIs. You don't get anything like Rails Console for free when you're building a custom Ruby program. So, when you start up an interactive environment, you may find yourself starting each session like so:

You may keep your console running just because you don't want to have to type all of that junk again. But eventually you will restart your computer, close your terminal, etc. and you will loose all of the work you put into setting up your console environment.

My question to you (and to myself), is why go through all this trouble when creating your own console is so easy! Just create a console file in the root of your project and insert all of that code that you would normally put into an interactive session. You can even get fancy with a little color output and various, generic helper methods that are useful for debugging. A console example from one of my [current projects] looks like the following:

In my console I:

That's quite a bit of work if I were to do that each time I started a new interactive Pry session. And to be honest, I just wouldn't. I would do the bare minimum required to test something. With the console, I can have some other niceties because I only have to write them once. So nice!

This has now become a mandatory step when getting started on any new Ruby project. I add some basics that I put into all of my console files and then I evolve the console as the project moves forward. I can't imagine working any other way (well... I can, I just don't prefer it).