March 13th, 2015


Adventures in Ruby: Variable Interpolation

I don't remember how I did programming in the days before Google and Stack Overflow. I think I spent a lot of time flipping through books. Or possibly actually memorized language syntax, which seems like a horrible waste of neurons that might be more profitably put to other uses.

However, if you want to get good results out of Google, you've got to know how to search for the right thing. There is a thing that I often want to do in Ruby, where I've got a variable that holds a string, and I want to put the value of that variable into another string. And somehow I always end up searching for something like "ruby string substitution" and getting pages and pages of stuff on the sub method, which is nice but not what I want at all.

So this is a note to myself: the thing you want to do is called "variable interpolation" and it works a little something like this:

thingy = "variable interpolation"
puts ("When you want to put a variable into a string, that's called #{thingy}.")