What's the right way to deal with this?
"Hi %s, today is %s so you get 10% OFF!" % ['Joe', 'Monday']
# => ArgumentError: malformed format string - %O
I can't use normal %{keyname}
or #{code}
because I'm parsing strings for urls and stripping them out so I can stick them back in different formats (with/without protocol, shortened, full length, etc). So the number of replacements is unknown and they don't have names. They are just an array of urls.
I also tried escaping the %
:
"Hi %s, today is %s so you get 10\% OFF!" % ['Joe', 'Monday']
# => ArgumentError: malformed format string - %O
but get the exact same result.
String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name.
The string interpolation result is 'Hello, World!' . You can put any expression inside the placeholder: either an operator, a function call, or even more complex expressions. ${n1 + n2} is a placeholder consisting of the addition operator and 2 operands.
In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
If the interpolation expression evaluates to null , an empty string ("", or String.
Two %'s
>> "Hi %s, today is %s so you get 10%% OFF!" % ['Joe', 'Monday']
=> "Hi Joe, today is Monday so you get 10% OFF!"
"Hi %s, today is %s so you get 10%% OFF!" % ['Joe', 'Monday']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With