首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Rinda::Template

Parent:Rinda::Tuple

模板用于匹配Rinda中的元组。

公共实例方法

===(tuple) 显示源

匹配的别名。

代码语言:javascript
复制
# File lib/rinda/rinda.rb, line 169
def ===(tuple)
  match(tuple)
end

匹配(元组)显示源

匹配此模板tuple。该tuple大小必须为模板相同。nil在模板中具有值的元素充当通配符,匹配元组中相应位置中的任何值。tuple如果是#==或=== ,则模板的元素匹配。

代码语言:javascript
复制
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
代码语言:javascript
复制
# File lib/rinda/rinda.rb, line 148
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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com