Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation when not using a string literal

Tags:

ruby

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?

like image 899
Mike Cargal Avatar asked Mar 14 '09 15:03

Mike Cargal


2 Answers

Actually Ruby has functionality very similar to John's Python example:

$ irb
>> greeting = 'hello %s, my name is %s!'
>> interpolated = greeting % ['Mike', 'John']
=> "hello Mike, my name is John!"
>>

This is also useful if your argument is an array constant. If you must use #{} style interpolation you could use eval:

>> greeting = 'hi #{name}'    # notice name is not defined yet
>> name = "mike"
>> eval '"' + greeting + '"'

The eval approach is going to be much slower than using % style interpolation, so it's a trade-off.

like image 195
Mark A. Nicolosi Avatar answered Sep 24 '22 07:09

Mark A. Nicolosi


I suggest that you look at Liquid templating language which provides more powerful features (e.g. you can reference parameters by name). Previous example would look like:

greeting = Liquid::Template.parse("hello {{your_name}}, my name is {{my_name}}!")
interpolated = greeting.render('your_name' => 'Mike', 'my_name' => 'John')
# => "hello Mike, my name is John!"
like image 21
Raimonds Simanovskis Avatar answered Sep 26 '22 07:09

Raimonds Simanovskis