Range.- ruby, .
for . case . , .- ruby
++ --. += 1. - Use the notation
"#{ }". It is much better and faster than using +. In fact, you can omit to_sit if you use it.
I would say:
vowel = 0
cons = 0
('a'..'z').each do |c|
case c
when 'a', 'e', 'i', 'o', 'u'; vowel += 1
else cons += 1
end
end
puts "Vowel: #{vowel}"
puts "Consonants: #{cons}"
If I wanted a shorter one, I could go with this:
partition = ('a'..'z').group_by{|c| c =~ /[aeiou]/}
puts "Vowel: #{partition[0].length}"
puts "Consonants: #{partition[nil].length}"
source
share