Sha256: d55ba1a6d8b8794a331da844fb29954c78c389a9808f59ade5ba9b0516e674ba

Contents?: true

Size: 953 Bytes

Versions: 4

Compression:

Stored size: 953 Bytes

Contents

require 'facets/hash/recursively'

class Hash
  def recursively_comparing(other_enum, path: [], &block)
    ComparingRecursor.new(self, other_enum, &block)
  end

  class ComparingRecursor < Enumerable::Recursor #:nodoc:
    def initialize(enum, other_enum, *types, &block)
      @other_enum = other_enum
      super(enum, *types, &block)
    end
    def method_missing(op, &yld)
      yld = yld    || lambda{ |k,v| [k,v] }  # ? to_enum
      rec = @block || yld #lambda{ |k,v| [k,v] }
      @enum.__send__(op) do |k,v|
        other_v = @other_enum.dig(k)
        #puts %(#{@enum.inspect}: k=#{k.inspect}, v=#{v.inspect}, other_v=#{other_v.inspect})
        case v
        when String # b/c of 1.8
          yld.call(k, v, other_v)
        when *@types
          res = v.recursively_comparing(other_v, &@block).__send__(op,&yld)
          rec.call(k, res, other_v)
        else
          yld.call(k, v, other_v)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
merge_params-0.5.0 lib/facets/hash/recursively_comparing.rb
merge_params-0.4.2 lib/facets/hash/recursively_comparing.rb
merge_params-0.4.1 lib/facets/hash/recursively_comparing.rb
merge_params-0.4.0 lib/facets/hash/recursively_comparing.rb