Class: LooseTightDictionary::Identity
- Inherits:
-
Object
- Object
- LooseTightDictionary::Identity
- 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
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
Instance Method Summary collapse
-
#identical?(str1, str2) ⇒ Boolean
Two strings are “identical” if they both match this identity and the captures are equal.
-
#initialize(regexp_or_str) ⇒ Identity
constructor
A new instance of Identity.
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
#regexp ⇒ Object (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.
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 |