Sha256: 047184dffd9f5bbd4e342321e3fa2b1eb2eb6382725b3565e80a8e900bf5ef45
Contents?: true
Size: 777 Bytes
Versions: 1
Compression:
Stored size: 777 Bytes
Contents
module RespondToDig class << self def respond_to_dig(receiver) receiver.tap do |r| r.singleton_class.send(:include, RespondToDig) if RespondToDig.diggable? r end end # The reason why not purely duck typed by `[]` method is to avoid unexpected behavior, # for example we won't get `'o'` by `'foo'.dig(1)` by String#[]. def diggable?(target) target.is_a? Enumerable and target.respond_to? :[] and not target.respond_to? :dig end end def dig(key, *rest) value = RespondToDig::respond_to_dig(self[key]) if value.nil? || rest.empty? value elsif value.respond_to?(:dig) value.dig(*rest) else fail TypeError, "#{value.class} does not have #dig method" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
respond_to_dig-1.0.1 | lib/respond_to_dig.rb |