| Class | Rinda::Template |
| In: |
lib/rinda/rinda.rb
|
| Parent: | Tuple |
Matches this template against tuple. The tuple must be the same size as the template. An element with a nil value in a template acts as a wildcard, matching any value in the corresponding position in the tuple. Elements of the template match the tuple if the are #== or #===.
Template.new([:foo, 5]).match Tuple.new([:foo, 5]) # => true Template.new([:foo, nil]).match Tuple.new([:foo, 5]) # => true Template.new([String]).match Tuple.new(['hello']) # => true Template.new([:foo]).match Tuple.new([:foo, 5]) # => false Template.new([:foo, 6]).match Tuple.new([:foo, 5]) # => false Template.new([:foo, nil]).match Tuple.new([:foo]) # => false Template.new([:foo, 6]).match Tuple.new([:foo]) # => false
# File lib/rinda/rinda.rb, line 148
148: def match(tuple)
149: return false unless tuple.respond_to?(:size)
150: return false unless tuple.respond_to?(:fetch)
151: return false unless self.size == tuple.size
152: each do |k, v|
153: begin
154: it = tuple.fetch(k)
155: rescue
156: return false
157: end
158: next if v.nil?
159: next if v == it
160: next if v === it
161: return false
162: end
163: return true
164: end