Reputation: 1709
How would I find and replace '49' when '49' will be an unknown id, using ruby on rails?
str = "select * from clients where client_id = 49 order by registration_date asc"
str = str.gsub(/someRegExThatFinds49/, replacement_id) # <------ Here's the concept
Looking for a syntax and example that's correct. Thanks.
Upvotes: 2
Views: 5787
Reputation: 80105
unknown_id = 49
puts "hello4849gone".gsub(/#{unknown_id}/, "HERE") #=> hello48HEREgone
Upvotes: 2
Reputation: 5714
This would work, using a copy of the string:
new_str = str.gsub(/\d+/, replacement_id)
Or, if you prefer to do it in place (modifying the string directly)
str.gsub!(/\d+/, replacement_id)
ian.
Upvotes: 3