More Ruby Miscellanea


I like to chronicle the little discoveries and revelations in my day’s work. It helps keep me clearheaded, gives me a reference for later, and lets me share my knowledge with the world. Granted, in large part, these “discoveries” have long since been “discovered” — but even in well-documented libraries and languages, it’s surprising how easily knowledge gets lost in the shuffle.


With that in mind, here are a few little bits of Ruby trivia I’ve run across in recent days. May they help you in your own work, too.

Varieties of string quoting

There are many ways to use strings in Ruby, with single quotes, double quotes, heredocs, and string interpolation.

Here’s a useful discussion on the topic from StackOverflow:


String interpolation in particular was foreign to me, and it turns out there are several different ways it can be used! In short it’s a useful way to get around escaping or changing characters such as quotation marks in your strings (and it can do other stuff too).


Heredocs in Ruby are a useful technique as well; it’s not unique to Ruby, of course, but a great option to have around for setting of blocks of text. It turns out, by the way, that heredocs don’t play well with hashes in Ruby, syntactically. It’s tough to make them work right, and you might be better off using a different type of quote in your hashes, even for longer strings. It took me a frustratingly long time to discover that particular item!

Anyway, here’s a nice breakdown of various heredoc features in Ruby.

Predicting remote files

Ruby is great at grabbing data from a remote file — you can treat data from an online file almost identically to how you read any other file input. That’s all well and good so long as you know the file type before you work with it. But what if you don’t?


That was the situation I ran across recently, and it had me stumped. So I thought about how to find out what the file was before you even grabbed the data — and it turns out there’s a pretty nifty way to do that that’s even in Ruby’s standard library! So it’s very easy to use. Here is the example that I found, and it’s very easy to adjust it to your needs.

Grep in hashes

You probably know grep; it’s that multi-use tool for finding and searching text. Well, turns out (as I discovered while searching on a whim) that you can in fact search your hashes by grepping them. Pretty cool stuff! It’s a little thing, but it’s dead useful in certain situations, like if you have formatted or parsed input coming in but don’t know exactly what to expect.



That’s it for now, but pushing yourself in the world of software engineering means you discover new challenges everyday — and need to solve them! So hang tight, and as always, thanks for reading!



Leave a Reply

Your email address will not be published. Required fields are marked *