Several months ago, I asked Program to count vowelsProgram to count vowels, which was asking for a code review of a vowel counting function in implemented in Python.
Since I've been trying to learn Ruby, I decided to reimplement the entire program in it. I'm mainly looking for critiques on:
- Naming conventions
- Best practices for string iteration
do/end
vs{}
- Coding standards and best practices
Of course there are some slight changes in how I implemented this in ruby vs Python. The biggest being simply using a Hash
since I couldn't find a suitable replacement for Python's tuples. On a unrelated side note, is there a replacement for Python's tuples in ruby?
def count_vowels(string)
# Create a function that will receive a string. The
# function will count each vowel in the string and
# return a count of all the vowels, whether found or not,
# each being a `key, value` in a Hash.
vowels = {"a" => 0, "e" => 0, "i" => 0, "o" => 0, "u" => 0}
string.each_char do |chr|
if vowels.has_key?(chr)
vowels[chr] += 1
end
end
return vowels
end