Skip to content

Instantly share code, notes, and snippets.

@rafops
Created February 23, 2012 14:19
Show Gist options
  • Save rafops/1893038 to your computer and use it in GitHub Desktop.
Save rafops/1893038 to your computer and use it in GitHub Desktop.
Traversing a Ruby Hash
class Hash
def traverse *args
args.inject(self) { |h,k| h = h[k] }
end
end
h = { a: { b: { c: "foobar" } } }
h.traverse :a, :b, :c # "foobar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment