Skip to content

Instantly share code, notes, and snippets.

@gnepud
Forked from dorkalev/a start
Created September 20, 2013 08:36
Show Gist options
  • Save gnepud/6634795 to your computer and use it in GitHub Desktop.
Save gnepud/6634795 to your computer and use it in GitHub Desktop.
sentence = "there is a wild rose"
letters = sentence.gsub(' ','').split(//)
letters_counter = {}
letters.each do |letter|
letters_counter[letter] ||= 0
letters_counter[letter] += 1
end
letters_counter = letters.each_with_object({}) do |letter, a|
a[letter] ||= 0
a[letter] += 1
end
letters_counter = letters.each_with_object(Hash.new(0)) do |letter, a|
a[letter] += 1
end
require 'yaml'
puts letters_counter.to_yaml
RESULT:
---
t: 1
h: 1
e: 3
r: 2
i: 2
s: 2
a: 1
w: 1
l: 1
d: 1
o: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment