Class: Rinda::Tuple
- Inherits:
-
Object
- Object
- Rinda::Tuple
- 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.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](k) ⇒ Object
Accessor method for elements of the tuple.
-
#each ⇒ Object
Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.
-
#fetch(k) ⇒ Object
Fetches item
k
from the tuple. -
#initialize(ary_or_hash) ⇒ Tuple
constructor
Creates a new Tuple from
ary_or_hash
which must be an Array or Hash. -
#size ⇒ Object
The number of elements in the tuple.
-
#value ⇒ Object
Return the tuple itself.
Constructor Details
#initialize(ary_or_hash) ⇒ Tuple
Creates a new Tuple from ary_or_hash
which must be an Array or Hash.
52 53 54 55 56 57 58 |
# 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) ⇒ Object
Accessor method for elements of the tuple.
70 71 72 |
# File 'lib/rinda/rinda.rb', line 70 def [](k) @tuple[k] end |
#each ⇒ Object
Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.
85 86 87 88 89 90 91 |
# 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) ⇒ Object
Fetches item k
from the tuple.
77 78 79 |
# File 'lib/rinda/rinda.rb', line 77 def fetch(k) @tuple.fetch(k) end |
#size ⇒ Object
The number of elements in the tuple.
63 64 65 |
# File 'lib/rinda/rinda.rb', line 63 def size @tuple.size end |
#value ⇒ Object
Return the tuple itself
95 96 97 |
# File 'lib/rinda/rinda.rb', line 95 def value @tuple end |