类 Rinda::Tuple

元组是 Rinda 编程中的基本对象。如果元组和模板大小相同,则可以将元组与模板进行匹配。

公共类方法

new(ary_or_hash) 点击切换源代码

ary_or_hash 创建一个新的 Tuple,它必须是 ArrayHash

# File lib/rinda/rinda.rb, line 53
def initialize(ary_or_hash)
  if hash?(ary_or_hash)
    init_with_hash(ary_or_hash)
  else
    init_with_ary(ary_or_hash)
  end
end

公共实例方法

[](k) 点击切换源代码

元组元素的访问器方法。

# File lib/rinda/rinda.rb, line 71
def [](k)
  @tuple[k]
end
each() { |k, v| ... } 点击切换源代码

遍历元组,生成索引或键以及值,从而确保数组与哈希的迭代方式类似。

# File lib/rinda/rinda.rb, line 86
def each # FIXME
  if Hash === @tuple
    @tuple.each { |k, v| yield(k, v) }
  else
    @tuple.each_with_index { |v, k| yield(k, v) }
  end
end
fetch(k) 点击切换源代码

从元组中获取项目 k

# File lib/rinda/rinda.rb, line 78
def fetch(k)
  @tuple.fetch(k)
end
size() 点击切换源代码

元组中元素的数量。

# File lib/rinda/rinda.rb, line 64
def size
  @tuple.size
end
value() 点击切换源代码

返回元组本身

# File lib/rinda/rinda.rb, line 96
def value
  @tuple
end

私有实例方法

hash?(ary_or_hash) 点击切换源代码
# File lib/rinda/rinda.rb, line 102
def hash?(ary_or_hash)
  ary_or_hash.respond_to?(:keys)
end
init_with_ary(ary) 点击切换源代码

ary 转换为有效的 Tuple

# File lib/rinda/rinda.rb, line 109
def init_with_ary(ary)
  @tuple = Array.new(ary.size)
  @tuple.size.times do |i|
    @tuple[i] = ary[i]
  end
end
init_with_hash(hash) 点击切换源代码

确保 hash 是有效的 Tuple

# File lib/rinda/rinda.rb, line 119
def init_with_hash(hash)
  @tuple = Hash.new
  hash.each do |k, v|
    raise InvalidHashTupleKey unless String === k
    @tuple[k] = v
  end
end