Class: Hash
Direct Known Subclasses
Instance Method Summary collapse
-
#metch(search, default = nil) ⇒ Object
Returns a value from the hash for the matching key.
Instance Method Details
#metch(search, default = nil) ⇒ Object
Returns a value from the hash for the matching key
Similar to fetch
, search the hash keys for the search string and return the corresponding value. Unlike fetch
, however, if a hash key is a Regexp, the search string is matched against this Regexp. The hash is searched in it’s natural order.
21 22 23 24 25 26 27 28 29 |
# File 'lib/core_ext/hash.rb', line 21 def metch(search, default=nil) fetch search rescue KeyError each do |key, value| next unless key.is_a? Regexp return value if search.match? key end default ? default : raise(KeyError, "no match found: #{search.inspect}") end |