Skip to content

Instantly share code, notes, and snippets.

@agnellvj
Created June 11, 2011 05:46
Show Gist options
  • Save agnellvj/1020297 to your computer and use it in GitHub Desktop.
Save agnellvj/1020297 to your computer and use it in GitHub Desktop.
data = ["1", "11", "111", "2", "22", "222", "3", "4", "5"]
y = (1..9).inject({}) { |memo, data| memo[data.to_s] = 0; memo }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
y = {};(1..9).map { |x| y[x.to_s] = 0 }
data.each { |z| y[z.chr] = y.fetch(z.chr, 0) + 1 }
puts y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment