Created
February 23, 2013 17:04
-
-
Save louim/5020463 to your computer and use it in GitHub Desktop.
#take a string and split it in hash, based on letter frequency
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
#take a string and split it in hash, based on letter frequency | |
class NameToHash | |
def initialize(name = "Louis-Michel") | |
@name = name.downcase | |
name_convert | |
end | |
def name_convert | |
hash = Hash.new | |
@name.each_char do |lettre| | |
if hash.has_key?(lettre) | |
hash[lettre] += 1 | |
else | |
hash[lettre] = 1 | |
end | |
end | |
puts hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tu pourrais profiter du constructeur de
Hash
qui prend un argument optionnel. Cet argument sera retourné si on appelleHash#[]
avec une clé inconnueRegarde Enumerable#inject, ça pourrait t’aider à refactorer ton code. C’est complexe, mais très puissant 😄