Sha256: 883f4943c0df30c8dbc2ff22c296df1fa3511466bdaed49b1666b92fb08370e8

Contents?: true

Size: 708 Bytes

Versions: 9

Compression:

Stored size: 708 Bytes

Contents

class LooseTightDictionary
  # Identities take effect when needle and haystack both match a regexp
  # Then the captured part of the regexp has to match exactly
  class Identity
    attr_reader :regexp
    
    def initialize(regexp_or_str)
      @regexp = regexp_or_str.to_regexp
    end
    
    # Two strings are "identical" if they both match this identity and the captures are equal.
    #
    # Only returns true/false if both strings match the regexp.
    # Otherwise returns nil.
    def identical?(str1, str2)
      if str1_match_data = regexp.match(str1) and match_data = regexp.match(str2)
        str1_match_data.captures == match_data.captures
      else
        nil
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
loose_tight_dictionary-1.0.5 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-1.0.4 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-1.0.3 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-1.0.2 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-1.0.1 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-1.0.0 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-0.2.3 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-0.2.2 lib/loose_tight_dictionary/identity.rb
loose_tight_dictionary-0.2.1 lib/loose_tight_dictionary/identity.rb