Ruby v Perl - interpollating variables
Archive - Originally posted on "The Horse's Mouth" - 2006-12-15 04:57:25 - Graham EllisWhen printing in Perl, you can drop a variable into a double quoted string ant it will be interpretter for you:
"--- $number ---"
If you want to perform an operation on the variable, though, it's not as easy - you have to come out of the double quoted string, do the calculation, then start another string:
"--- ",$number + 1," ---"
In Ruby, variable names do NOT start with a $ so you can't simply drop them in to your double quoted string. Instead, you can write #{expression} within the double quotes and that gives you ...
"--- #{number + 1} ---"
And that's fabulous - it gives you the ability to write any expresion you want, or just a simple variable, with a double quoted string and have the same simple syntax for both.