I have the following variables:
query = "(first_name = ?) AND (country = ?)" # string
values = ['Bob', 'USA'] # array
I need the following result:
result = "(first_name = Bob) AND (country = USA)" # string
The number of substitutions varies (1..20).
What is the best way to do it in Ruby?
If you don't mind destroying the array:
query.gsub('?') { values.shift }
Otherwise just copy the array and then do the replacements.
Edited: Used gsub('?') instead of a regex.
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