class Rinda::Template
模板用于在 Rinda 中匹配元组。
公共实例方法
===(tuple) 点击切换源代码
match 的别名。
# File rinda-0.2.0/lib/rinda/rinda.rb, line 171 def ===(tuple) match(tuple) end
match(tuple) 点击切换源代码
将此模板与 tuple 进行匹配。 tuple 的大小必须与模板相同。模板中具有 nil 值的元素充当通配符,匹配元组中相应位置的任何值。如果模板的元素与 tuple 的元素相等 (==) 或满足 ===,则匹配。
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 rinda-0.2.0/lib/rinda/rinda.rb, line 150 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