gpt4 book ai didi

ruby - 获取 { } 大括号之间的内容

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:48 25 4
gpt4 key购买 nike

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

I love {you}

如何获取元素“you”?如果我想替换内容,比如说把“你”改成“她”,我应该怎么做?可能使用 gsub

最佳答案

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!

关于ruby - 获取 { } 大括号之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11944025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com