【发布时间】:2016-12-29 18:52:57
【问题描述】:
有没有办法用变量替换下面代码中的 [aeiou]?询问的原因是我以后是否必须使用辅音来执行此操作,似乎最好定义变量并在正则表达式中使用它,因为我认为它会更干净。
def word_mutation(word)
vowels = %w[a e i o u]
if word.match(/^[aeiou]/)
return word
else
return "consonant-starter"
end
end
所以,我再次尝试使用或只是好奇尝试使用类似的东西:
if word.match(/^vowels/)
谢谢!!
【问题讨论】:
-
irb(main):006:0> word = "vowels" ; myvar = "元音" ; word.match(/^#{myvar}/