Class: Rinda::Tuple
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/rinda/rinda.rb |
Overview
A tuple is the elementary object in ::Rinda programming. Tuples may be matched against templates if the tuple and the template are the same size.
Class Method Summary
-
.new(ary_or_hash) ⇒ Tuple
constructor
Creates a new
Tuple
fromary_or_hash
which must be an Array or Hash.
Instance Method Summary
-
#[](k)
Accessor method for elements of the tuple.
-
#each
Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.
-
#fetch(k)
Fetches item
k
from the tuple. -
#size
The number of elements in the tuple.
-
#value
Return the tuple itself.
- #hash?(ary_or_hash) ⇒ Boolean private
-
#init_with_ary(ary)
private
Munges
ary
into a validTuple
. -
#init_with_hash(hash)
private
Ensures
hash
is a validTuple
.
Constructor Details
.new(ary_or_hash) ⇒ Tuple
Creates a new Tuple
from ary_or_hash
which must be an Array or Hash.
# File 'lib/rinda/rinda.rb', line 52
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
Instance Method Details
#[](k)
Accessor method for elements of the tuple.
# File 'lib/rinda/rinda.rb', line 70
def [](k) @tuple[k] end
#each
Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.
# File 'lib/rinda/rinda.rb', line 85
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)
Fetches item k
from the tuple.
# File 'lib/rinda/rinda.rb', line 77
def fetch(k) @tuple.fetch(k) end
#hash?(ary_or_hash) ⇒ Boolean
(private)
# File 'lib/rinda/rinda.rb', line 101
def hash?(ary_or_hash) ary_or_hash.respond_to?(:keys) end
#init_with_ary(ary) (private)
Munges ary
into a valid Tuple
.
#init_with_hash(hash) (private)
Ensures hash
is a valid Tuple
.
# File 'lib/rinda/rinda.rb', line 118
def init_with_hash(hash) @tuple = Hash.new hash.each do |k, v| raise InvalidHashTupleKey unless String === k @tuple[k] = v end end
#size
The number of elements in the tuple.
# File 'lib/rinda/rinda.rb', line 63
def size @tuple.size end
#value
Return the tuple itself
# File 'lib/rinda/rinda.rb', line 95
def value @tuple end