Class: LooseTightDictionary::Identity

Inherits:
Object
  • Object
show all
Defined in:
lib/loose_tight_dictionary/identity.rb

Overview

Identities take effect when needle and haystack both match a regexp Then the captured part of the regexp has to match exactly

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp_or_str) ⇒ Identity

Returns a new instance of Identity.



7
8
9
# File 'lib/loose_tight_dictionary/identity.rb', line 7

def initialize(regexp_or_str)
  @regexp = regexp_or_str.to_regexp
end

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



5
6
7
# File 'lib/loose_tight_dictionary/identity.rb', line 5

def regexp
  @regexp
end

Instance Method Details

#identical?(str1, str2) ⇒ Boolean

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.

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/loose_tight_dictionary/identity.rb', line 15

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