【发布时间】:2016-01-26 00:23:59
【问题描述】:
我有一个这样的字符串:
'show -drum-, -drum- and -drum-'
每个/-[a-z]+-/ 键的选项数组:
{
'-drum-' => %w{kick snare hihat crash},
...
}
我想进行所有可能的替换,包括:
[
'show kick, snare and hihat',
'show kick, hihat and snare',
'show snare, hihat and kick',
'show snare, kick and hihat',
'show hihat, snare and kick',
'show hihat, kick and snare',
'show crash, kick and snare',
'show crash, snare and kick',
'show hihat, snare and crash',
...
]
理想情况下,这会发生而无需任何重复,尽管我会接受会返回的解决方案,例如“show kick, kick and kick”等。
哈希中还有其他键替换,所以我也必须对它们进行独特的组合。
【问题讨论】:
-
欢迎来到 Stack Overflow。 “......我会接受解决方案......” - 我们希望看到您为解决问题所做的努力。你试过什么?为什么它不起作用?如果您还没有尝试过,假设我们会编写解决方案是不好的,因此请向我们展示您的工作以供我们纠正,而不是从头开始编写一些东西。阅读“How to Ask”和“minimal reproducible example”。
标签: ruby string substitution