-
-
Save gnepud/6634795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentence = "there is a wild rose" | |
letters = sentence.gsub(' ','').split(//) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letters_counter = {} | |
letters.each do |letter| | |
letters_counter[letter] ||= 0 | |
letters_counter[letter] += 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letters_counter = letters.each_with_object({}) do |letter, a| | |
a[letter] ||= 0 | |
a[letter] += 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
letters_counter = letters.each_with_object(Hash.new(0)) do |letter, a| | |
a[letter] += 1 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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