【发布时间】:2021-12-07 15:41:54
【问题描述】:
我正在尝试用数组中的相应值替换字符串中的单词(更一般地说是字符序列)。一个例子是:
"The dimension of the square is {{width}} and {{length}}" 和数组 [10,20] 应该给
"The dimension of the square is 10 and 20"
我尝试使用 gsub 作为
substituteValues.each do |sub|
value.gsub(/\{\{(.*?)\}\}/, sub)
end
但我无法让它工作。我还考虑过使用哈希而不是数组,如下所示:
{"{{width}}"=>10, "{{height}}"=>20}。我觉得这可能会更好,但我不知道如何编码(红宝石新手)。任何帮助表示赞赏。
【问题讨论】:
标签: ruby-on-rails regex ruby string hash