String interpolation with actual % in string

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.

Answers


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']

Need Your Help

How to get class object's name as a string in Javascript?

javascript class

Let's say I instantiate an object in Javascript like this:

How to strip a specific word from a string?

python string strip

I need to strip a specific word from a string.