Skip to content

Instantly share code, notes, and snippets.

@procload
Created May 17, 2013 19:15
Show Gist options
  • Save procload/5601294 to your computer and use it in GitHub Desktop.
Save procload/5601294 to your computer and use it in GitHub Desktop.
def count_letters(given_string)
given_arr = given_string.split('')
puts "String length: #{given_arr.length}"
hsh = {}
given_arr.each do |character|
hsh[character] = hsh.fetch(character,0) + 1
end
hsh.each_pair do |key, val|
puts "The character #{key} appears #{val} times."
end
end
count_letters("This is my string, yo. How do you like it?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment