Class
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 Methods
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.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
Instance Methods
#
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rinda/rinda.rb, line 71
def [](k)
@tuple[k]
end
Accessor method for elements of the tuple.
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.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
Iterate through the tuple, yielding the index or key, and the value, thus ensuring arrays are iterated similarly to hashes.
#
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rinda/rinda.rb, line 78
def fetch(k)
@tuple.fetch(k)
end
Fetches item k
from the tuple.
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rinda/rinda.rb, line 102
def hash?(ary_or_hash)
ary_or_hash.respond_to?(:keys)
end
No documentation available
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.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
Munges ary
into a valid Tuple
.
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.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
Ensures hash
is a valid Tuple
.
#
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rinda/rinda.rb, line 64
def size
@tuple.size
end
The number of elements in the tuple.
#
lib/rinda/rinda.rb
View on GitHub
# File tmp/rubies/ruby-3.3.0/lib/rinda/rinda.rb, line 96
def value
@tuple
end
Return the tuple itself