Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How do I include a variable in the 'replace' portion of gsub?
gsub
replace.gsub(/#{year}","1/, '#{year}","b')
This outputs:
=> #{year}","b
Let's say year = 2013. I want it to output:
=> 2013","b
'#{year}","b'
"#{year}\",\"b"
Adding on to Blender's answer, you can use an alternate way of writing strings to avoid having to escape quotes:
replace.gsub(/#{year}","1/, %{#{year}","b})
where %{} is another way to write a string literal that you can do string interpolation in.
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
'#{year}","b'with"#{year}\",\"b".