Why you should spend some time configuring irb
Many of us have fallen in love with Ruby playing with irb. I’m such a person. Ruby is a wonderful and powerful language, and you have to play with its features many times to really understand what you can achieve with it. So I think we all agree on the importance of irb. The interactive console is not only a way to familiarize with the language but, in many cases, is a great tool to improve one’s productivity. Indeed, it happens many times a day that you run irb just to solve a doubt or to try an API.
Why should I configure it?
In my opinion irb is powerful enough even without any particular configuration. I have been used to a raw irb for a long time and I think you could use it in an efficient way. But I still think you should spend some time configuring irb. The reasons are simple. Firstly, having personal irb configurations is a boost for productivity. There are many tasks you perform repeatedly when you are playing with an API or when you are trying Ruby features. Configuring irb while focusing on your needs will save your time. Secondly, spending time reading other configurations or thinking about your needs is a very good learning opportunity.
The aim of this post is not Hey folks, have a look at my config, it’s so beautiful! but I think we can use my config as an example of what you can achieve configuring irb. I don’t like to recommend my configs to other people but I won’t stop recommending making your tools more personal. Based on my experience, a personal toolbox is a good way to boost productivity.
What you can’t miss
Although I wouldn’t recommend any particular configuration, there are still some things I think you should know about. As the history for example, you want it, don’t you? Well, you can get the irb history in various ways. I show the one I prefer below:
I think there isn’t much to comment here. Irb history works quite like a sane history does, you type something and then you can re-execute what you have typed using up/down cursor to move in your history. Another thing you surely want is bash-like completion, the first require gives completion to you. Personally, I adore this feature :)
I think the above mentioned features are the only ones I would consider generally unmissable but, consider it a matter of taste if you want, there is a little gem I also consider unmissable and it’s map_by_method. Our dear drnic wrote it some time ago and it’s still a classic. I think it’s simpler to show a few examples that explain how it works with words. Here we go:
I think the example shown are self-describing, aren’t they? :)
So we talked about what I believe unmissable, now we can take a look at some stuff that makes the configuring interesting. As a rails developer, I use the console many times a day and I use it mainly to investigate on things I don’t understand well. So, while thinking about improving my use of console, I understood that I use some console patterns like create an array and test the foo method or create an hash to test the bar method. And very often I compare methods results on arrays or hashes. I think this is ordinary stuff we all do with the console and I thought I needed a way to create toys objects more quickly than typing something like the following:
So after a bit of time I came up with the following:
Well, that isn’t very magic Ruby but it is very useful and it represented for me a tiny opportunity to learn something “new”:https://gist.github.com/807492.
If you are a Rails developer the odds are you use irb to investigate ActiveRecord, in particular you would like to take a look at the SQL queries generated by our beloved framework. Surely you can do something like the following:
and solve your problem but I see another opportunity to dig into irb. The finest solution might be to redirect ActiveRecord logging in irb only when it runs in a rails environment. Loading a file only when irb runs in a rails environment is a very common problem, googling you can find many ways to solve it. I solved it in the following way:
I wrote the rails? method because I’ve planned to do other rails-related stuff in my “irbrc”:https://github.com/lucapette/dotfiles/blob/master/irbrc. The file loaded contains the methods that deal with ActiveRecord logging:
I’ve chosen to enable logging by default because I really like the resulting behaviour:
A simple conclusion
I think it’s fair to say we’ve covered most of the unmissable configuration experiences. On this short path, I’ve learnt many things and they procreate things I’ve planned to learn. Furthermore, while trying to implement the features we have talked about, I read many configs, posts and articles. These are the reason why I think you should spend some time configuring irb. You will give a boost to your productivity and you will improve your knowledge. A perfect match :)