class Rinda::Tuple

Public Class Methods

new(ary_or_hash) click to toggle source

Creates a new Tuple from ary_or_hash which must be an Array or Hash.

# File lib/rinda2/rinda.rb, line 16
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

Public Instance Methods

[](k) click to toggle source

Accessor method for elements of the tuple.

# File lib/rinda2/rinda.rb, line 34
def [](k)
  @tuple[k]
end
each() { |k, v| ... } click to toggle source

Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.

# File lib/rinda2/rinda.rb, line 49
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) click to toggle source

Fetches item k from the tuple.

# File lib/rinda2/rinda.rb, line 41
def fetch(k)
  @tuple.fetch(k)
end
size() click to toggle source

The number of elements in the tuple.

# File lib/rinda2/rinda.rb, line 27
def size
  @tuple.size
end
value() click to toggle source

Return the tuple itself

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

Private Instance Methods

hash?(ary_or_hash) click to toggle source
# File lib/rinda2/rinda.rb, line 65
def hash?(ary_or_hash)
  ary_or_hash.respond_to?(:keys)
end
init_with_ary(ary) click to toggle source

Munges ary into a valid Tuple.

# File lib/rinda2/rinda.rb, line 72
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) click to toggle source

Ensures hash is a valid Tuple.

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