user1245233 user1245233 - 5 months ago 16
Ruby Question

Remove braces in Ruby

I want to remove the braces so "{test}" becomes "test".
I tried

"{test}".gsub(/\{(.*)\}/,$1)
while "{test}".gsub!(/(\{).*(\})/,""); end
"{test}".gsub(/\{.*\}/,"")


Nothing seems to work.

Answer

You have to use \1, not $1

"{test}".gsub /\{(.*)\}/, '\1'

Or, if you just want to delete all curly braces:

"{test}".delete "{}"