As a developer, I am alway seeking for tools and tricks that allow me to be more efficient while working on projects. As a result, I've adopted some lesser known ruby interactive shell command. One of which is the underscore or
_. I found that this command when applied can be super beneficial and boost your productivity!
The underscore simply gives you the return value of the last evaluation made in the console. I find this especially useful in the event I forget to assign the output of a block of code or when I perform a query via Rails that I’d rather not run again. Let me show you what I mean:
Above is a screenshot detailing an example use case. In that example, I evaluated a block of code with a 5-second pause (to simulate a slow operation) without assigning it to the variable animal. Nothing to worry about, you can just use the underscore to avoid having to execute that code again!
Another fairly common situation were you might benefit from using the
_ method is when you run a query in Rails... but forgot to assign it to a variable!
The underscore is a useful tool to use when you are working in the ruby interactive shell. It may not seem like a lot of time to just re-run slow code… but it does add up!
What other kinds of hidden ruby tricks are there out? Please comment on this post if you know any and are in a charitable mood.