Skip to content

Instantly share code, notes, and snippets.

@zuchmanski
Created April 30, 2012 06:26
Show Gist options
  • Save zuchmanski/2555974 to your computer and use it in GitHub Desktop.
Save zuchmanski/2555974 to your computer and use it in GitHub Desktop.
Ruby quick tips
Hash[['foo', 'bar', 'foo', 'baz'].group_by {|s| s}.map {|k, v| [k, v.size]}]
#=> {"foo"=>2, "bar"=>1, "baz"=>1}
['foo', 'bar', 'foo', 'baz'].each_with_object({}) { |v, o| o[v] ||= 0; o[v] += 1 }
#=> {"foo"=>2, "bar"=>1, "baz"=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment