Hidden Ruby Syntax That Will Impress Your Cat

Edwin Mak
Edwin Mak
Sep 8, 2018 · 2 min read

Disclaimer; Your cat probably will NOT be impressed.

However, your coding co-workers looking over your shoulder while you bang out some code might if you use the lesser known underscore method! The underscore method simply gives you the return value of the last evaluation made in the console.

The underscore method 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.

An Example

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!

Conclusion

The underscore is a useful method to use when you are working in a Ruby terminal. Even though using this may not impress a cat or your fellow coding co-workers it might save you a lot of time. It may not seem like a lot of time to just re-run 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.

Edwin Mak

Written by

Edwin Mak

Software Developer dabbling with this and that.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade