Class: Rinda::Template
Overview
Templates are used to match tuples in Rinda.
Instance Method Summary collapse
-
#===(tuple) ⇒ Object
Alias for #match.
-
#match(tuple) ⇒ Object
Matches this template against
tuple
.
Methods inherited from Tuple
#[], #each, #fetch, #initialize, #size, #value
Constructor Details
This class inherits a constructor from Rinda::Tuple
Instance Method Details
#===(tuple) ⇒ Object
Alias for #match.
134 135 136 |
# File 'lib/rinda2/rinda.rb', line 134 def ===(tuple) match(tuple) end |
#match(tuple) ⇒ Object
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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rinda2/rinda.rb', line 113 def match(tuple) return false unless tuple.respond_to?(:size) return false unless tuple.respond_to?(:fetch) return false unless self.size == tuple.size each do |k, v| begin it = tuple.fetch(k) rescue return false end next if v.nil? next if v == it next if v === it return false end return true end |