Class Rinda::Tuple
In: lib/rinda/rinda.rb
Parent: Object

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.

Methods

[]   each   fetch   hash?   init_with_ary   init_with_hash   new   size   value  

Public Class methods

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

[Source]

    # File lib/rinda/rinda.rb, line 51
51:     def initialize(ary_or_hash)
52:       if hash?(ary_or_hash)
53:         init_with_hash(ary_or_hash)
54:       else
55:         init_with_ary(ary_or_hash)
56:       end
57:     end

Public Instance methods

Accessor method for elements of the tuple.

[Source]

    # File lib/rinda/rinda.rb, line 69
69:     def [](k)
70:       @tuple[k]
71:     end

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

[Source]

    # File lib/rinda/rinda.rb, line 84
84:     def each # FIXME
85:       if Hash === @tuple
86:         @tuple.each { |k, v| yield(k, v) }
87:       else
88:         @tuple.each_with_index { |v, k| yield(k, v) }
89:       end
90:     end

Fetches item k from the tuple.

[Source]

    # File lib/rinda/rinda.rb, line 76
76:     def fetch(k)
77:       @tuple.fetch(k)
78:     end

The number of elements in the tuple.

[Source]

    # File lib/rinda/rinda.rb, line 62
62:     def size
63:       @tuple.size
64:     end

Return the tuple itself

[Source]

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

Private Instance methods

[Source]

     # File lib/rinda/rinda.rb, line 100
100:     def hash?(ary_or_hash)
101:       ary_or_hash.respond_to?(:keys)
102:     end

Munges ary into a valid Tuple.

[Source]

     # File lib/rinda/rinda.rb, line 107
107:     def init_with_ary(ary)
108:       @tuple = Array.new(ary.size)
109:       @tuple.size.times do |i|
110:         @tuple[i] = ary[i]
111:       end
112:     end

Ensures hash is a valid Tuple.

[Source]

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

[Validate]