Created
April 30, 2012 06:26
-
-
Save zuchmanski/2555974 to your computer and use it in GitHub Desktop.
Ruby quick tips
This file contains hidden or 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
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