str = "Find the vowels in this string or else I'll date your sister"
我想计算一个字符串中元音的数量,我相信我已经做到了,但我是通过将每个字母附加到一个数组并获取数组的长度来完成的。什么是更常见的方法来做到这一点。也许用+=?
str.chars.to_a.each do |i|
if i =~ /[aeiou]/
x.push(i)
end
end
x.length
最佳答案
但这里有更好的答案 =)。原来我们有一个String#count
方法:
str.downcase.count 'aeiou'
#=> 17
关于ruby - 在 Ruby 中搜索元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24835078/