Last active
August 29, 2015 14:23
-
-
Save domgetter/615094aa7be44c2db81f to your computer and use it in GitHub Desktop.
I swear there's a use for these.
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
class Hash | |
def decompose | |
each_pair.each_with_index.each_with_object([{},{}]) { |((k,v), i), (f, s)| f[k], s[i] = i, v } | |
end | |
def decompose_to_a | |
each_pair.each_with_index.each_with_object([{},[]]) { |((k,v), i), (f, s)| f[k], s[i] = i, v } | |
end | |
end | |
{:a => "hello", :b => "world"}.decompose | |
#=> [{:a=>0, :b=>1}, {0=>"hello", 1=>"world"}] | |
{name: "John J", email: "[email protected]"}.decompose | |
#=> [{:name=>0, :email=>1}, {0=>"John J", 1=>"[email protected]"}] | |
{name: "John J", email: "[email protected]"}.decompose_to_a | |
#=> [{:name=>0, :email=>1}, ["John J", "[email protected]"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment