在{}大括号之间获取内容

Bru*_*Lin 1 ruby

如何在Ruby中的"{}"之间获取内容?例如,

我爱你}

我如何获取"你"元素?如果我想要替换内容,请将"你"更改为"她",我应该怎么做?可能用gsub

Ale*_*yne 6

replacements = {
  'you' => 'her',
  'angels' => 'demons',
  'ice cream' => 'puppies',
}

my_string = "I love {you}.\nYour voice is like {angels} singing.\nI would love to eat {ice cream} with you sometime!"

replacements.each do |source, replacement|
  my_string.gsub! "{#{source}}", replacement
end

puts my_string
# => I love her.
# => Your voice is like demons singing.
# => I would love to eat puppies with you sometime!
Run Code Online (Sandbox Code Playgroud)