Skip to content

Instantly share code, notes, and snippets.

@amatsuda
Created March 8, 2012 09:53
Show Gist options
  • Select an option

  • Save amatsuda/2000037 to your computer and use it in GitHub Desktop.

Select an option

Save amatsuda/2000037 to your computer and use it in GitHub Desktop.
class Hash
def try(*a, &b)
if (a.size == 1) && a.first.is_a?(Array) && (a.first.size == 1)
self.fetch a.first.first
else
super
end
end
end
# {a: 1, b: 2}.try([:a]) #=> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment