Class: Matchi::Be
- Inherits:
-
Object
- Object
- Matchi::Be
- Defined in:
- lib/matchi/be.rb
Overview
Identity matcher that checks if two objects are the exact same instance.
This matcher verifies object identity using Ruby’s Object#equal? method, which compares object IDs to determine if two references point to the exact same object in memory. This is different from equality comparison (==) which compares values.
Instance Method Summary collapse
-
#initialize(expected) ⇒ Be
constructor
Initialize the matcher with a reference object.
-
#match? { ... } ⇒ Boolean
Checks if the yielded object is the same instance as the expected object.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected) ⇒ Be
Initialize the matcher with a reference object.
43 44 45 |
# File 'lib/matchi/be.rb', line 43 def initialize(expected) @expected = expected end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the yielded object is the same instance as the expected object.
This method uses Ruby’s Object#equal? method, which performs identity comparison by comparing object IDs. Two objects are considered identical only if they are the exact same instance in memory.
67 68 69 70 71 |
# File 'lib/matchi/be.rb', line 67 def match? raise ::ArgumentError, "a block must be provided" unless block_given? @expected.equal?(yield) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
81 82 83 |
# File 'lib/matchi/be.rb', line 81 def to_s "be #{@expected.inspect}" end |