Class: Farce::Concurrent::Tuple
- Inherits:
-
Object
- Object
- Farce::Concurrent::Tuple
- Includes:
- Enumerable, Shareable::Delegated
- Defined in:
- lib/farce/concurrent/tuple.rb
Overview
A replacement for Concurrent::Tuple from concurrent-ruby.
Instance Attribute Summary collapse
-
#size ⇒ Integer
readonly
The size of the tuple.
Instance Method Summary collapse
-
#compare_and_set(index, old_value, new_value) ⇒ Boolean
(also: #cas)
Atomically sets the value at the given index to
new_valueif the current value is equal toold_value. -
#each {|value| ... } ⇒ Enumerator, self
Iterates over each element in the tuple.
-
#get(index) ⇒ BasicObject?
(also: #volatile_get)
Retrieves the value at the given index, or
nilif the index is out of bounds. -
#initialize(size) ⇒ Tuple
constructor
A new instance of Tuple.
-
#set(index, value) ⇒ BasicObject?
(also: #volatile_set)
Sets the value at the given index, or does nothing if the index is out of bounds.
Methods included from Shareable
Constructor Details
Instance Attribute Details
#size ⇒ Integer (readonly)
Returns the size of the tuple.
14 15 16 |
# File 'lib/farce/concurrent/tuple.rb', line 14 def size @size end |
Instance Method Details
#compare_and_set(index, old_value, new_value) ⇒ Boolean Also known as: cas
Atomically sets the value at the given index to new_value if the current value is equal to old_value.
41 42 43 44 |
# File 'lib/farce/concurrent/tuple.rb', line 41 def compare_and_set(index, old_value, new_value) return false unless index.between?(0, @size - 1) @tuple.compare_and_set(index, old_value, new_value) end |
#each {|value| ... } ⇒ Enumerator, self
Iterates over each element in the tuple.
50 51 52 53 54 |
# File 'lib/farce/concurrent/tuple.rb', line 50 def each(&) return enum_for(:each) unless block_given? @tuple.each(&) self end |
#get(index) ⇒ BasicObject? Also known as: volatile_get
Retrieves the value at the given index, or nil if the index is out of bounds.
26 |
# File 'lib/farce/concurrent/tuple.rb', line 26 def get(index) = index.between?(0, @size - 1) ? @tuple[index] : nil |
#set(index, value) ⇒ BasicObject? Also known as: volatile_set
Sets the value at the given index, or does nothing if the index is out of bounds.
33 |
# File 'lib/farce/concurrent/tuple.rb', line 33 def set(index, value) = index.between?(0, @size - 1) ? @tuple[index] = value : nil |