gpt4 book ai didi

ruby - 在 Ruby 中显示所有子字符串替换选项

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:21 26 4
gpt4 key购买 nike

<分区>

我正在尝试显示子字符串替换的所有可能选项。例如,如果我有这个字符串:

my_string = “yellow dogs are cooler than brown cats”

以下是可以替换的不同选项:

substitutions = {“yellow” => “black”, “brown” => “grey”}

我如何获得以下输出?

["black dogs are cooler than brown cats", "black dogs are cooler than grey cats", "yellow dogs are cooler than brown cats", "yellow dogs are cooler than grey cats"]

到目前为止,我只有这个,正如您所看到的,它并没有获得所有选项:

substitutions.each do |key, value|
puts my_string.sub(key, value) if my_string.include? key
end

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