I have a Ruby script that used string interpolation to build error messages.
p '#{vName} is not a defined variable' => 'xxx is not a defined variable'
Another programmer came through and attempted to externalize the string literals to a separate configuration file. Of course, he doesn’t get the substitution.
p err_string_from_config => '#{vName} is not a defined variable'
I’ve looked around, but couldn’t come up with anything better than converting to sprintf strings and using printf.
Does anybody know how to get the #{} substitution to work on strings that are not double quote literals within the Ruby script?
Actually Ruby has functionality very similar to John’s Python example:
This is also useful if your argument is an array constant. If you must use #{} style interpolation you could use eval:
The eval approach is going to be much slower than using % style interpolation, so it’s a trade-off.