?

Log in

No account? Create an account
wshaffer
Adventures in Ruby: Variable Interpolation 
13th-Mar-2015 01:49 pm
RTFM
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}.")
Comments 
13th-Mar-2015 10:36 pm (UTC)
I have this problem frequently with Google, that if I don't know the right terminology I get pages and pages of something that is related but not quite right, because Google assumes I knew what I was doing. It'd be great to have a "I don't exactly know what I'm looking for is called" mode that went for breadth rather than depth.
14th-Mar-2015 02:03 am (UTC)
I have kinda the opposite problem. For the past few years, Google has been trying to guess what you might mean for each word rather than just searching for the word itself, so several times a day when I'm searching for some exact word I have to go back and put the word in quotes because Google thinks I'm misspelling something.
14th-Mar-2015 02:43 am (UTC)
I agree, the non-exact matches when I want exactness are a problem, I want "close in meaning" search not "nearest neighbor word" search.
14th-Mar-2015 02:06 am (UTC)
AFAIK only Perl and Ruby call this "interpolation". It's "expansion" (a broader concept) in shells and "formatting" (subtly different concept) in Python and most other languages.

Oddly enough, you'll probably get the most reliable Google results for this question if you pretend you don't know anything and put it in language that is as untechnical as possible. E.g., {put variable in string ruby} works well.
14th-Mar-2015 02:11 am (UTC)
Speaking of shells and terminology that's hard to search for: I can never remember the name for process substitution or how to find it in docs.
14th-Mar-2015 04:13 am (UTC)
A whole set of slightly confusing conversations I've had with Python developers has suddenly become clear. (We were rewriting error message strings together, and I kept wondering why they kept talking about "formatting" when nothing was being formatted in the sense that I understood that term. I never bothered to clear up the misunderstanding because it didn't seem to prevent me from being able to edit the strings correctly.)
This page was loaded Oct 23rd 2017, 4:39 am GMT.