Skip to content

Instantly share code, notes, and snippets.

@budu
Created June 16, 2015 20:43
Show Gist options
  • Save budu/98fd7a45dfc614e6e1a7 to your computer and use it in GitHub Desktop.
Save budu/98fd7a45dfc614e6e1a7 to your computer and use it in GitHub Desktop.
Hash transpose
Hash.class_eval do
def transpose
reduce({}) do |rmemo, (rk, rv)|
rv.reduce(rmemo) do |cmemo, (ck, cv)|
cmemo[ck] ||= {}
cmemo[ck][rk] = cv
cmemo
end
rmemo
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment