Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Created May 11, 2015 20:16
Show Gist options
  • Save glinesbdev/aa077fd787a75de3ff90 to your computer and use it in GitHub Desktop.
Save glinesbdev/aa077fd787a75de3ff90 to your computer and use it in GitHub Desktop.
String count hash Ruby
def key_val_count(string)
obj = {}
string.each_char { |x| obj[x] = x.size }
puts obj
end
key_val_count('hello')
# => {"h"=>1, "e"=>1, "l"=>1, "o"=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment