class Rinda::Tuple
元组是 Rinda
编程中的基本对象。如果元组和模板的大小相同,则可以使用模板来匹配元组。
公共类方法
new(ary_or_hash) 点击以切换源代码
从 ary_or_hash
创建一个新的 Tuple
,ary_or_hash
必须是 Array 或 Hash。
# File rinda-0.2.0/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 rinda-0.2.0/lib/rinda/rinda.rb, line 71 def [](k) @tuple[k] end
each() { |k, v| ... } 点击以切换源代码
遍历元组,产生索引或键以及值,从而确保数组的迭代方式与哈希类似。
# File rinda-0.2.0/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 rinda-0.2.0/lib/rinda/rinda.rb, line 78 def fetch(k) @tuple.fetch(k) end
size() 点击以切换源代码
元组中元素的数量。
# File rinda-0.2.0/lib/rinda/rinda.rb, line 64 def size @tuple.size end
value() 点击以切换源代码
返回元组本身
# File rinda-0.2.0/lib/rinda/rinda.rb, line 96 def value @tuple end
私有实例方法
hash?(ary_or_hash) 点击以切换源代码
# File rinda-0.2.0/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 rinda-0.2.0/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 rinda-0.2.0/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