Module: Concurrent::MutableStruct
- Defined in:
- lib/concurrent-ruby/concurrent/mutable_struct.rb
Overview
An thread-safe variation of Ruby’s standard ‘Struct`. Values can be set at construction or safely changed at any time during the object’s lifecycle.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality.
-
#[](member) ⇒ Object
Attribute Reference.
-
#[]=(member, value) ⇒ Object
Attribute Assignment.
-
#each {|value| ... } ⇒ Object
Yields the value of each struct member in order.
-
#each_pair {|member, value| ... } ⇒ Object
Yields the name and value of each struct member in order.
-
#inspect ⇒ String
(also: #to_s)
Describe the contents of this struct in a string.
-
#merge(other) {|member, selfvalue, othervalue| ... } ⇒ Synchronization::AbstractStruct
Returns a new struct containing the contents of ‘other` and the contents of `self`.
-
#select {|value| ... } ⇒ Array
Yields each member value from the struct to the block and returns an Array containing the member values from the struct for which the given block returns a true value (equivalent to ‘Enumerable#select`).
-
#to_h ⇒ Hash
Returns a hash containing the names and values for the struct’s members.
-
#values ⇒ Array
(also: #to_a)
Returns the values for this struct as an Array.
-
#values_at(*indexes) ⇒ Object
Returns the struct member values for each selector as an Array.
Instance Method Details
#==(other) ⇒ Boolean
Equality
128 129 130 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 128 def ==(other) synchronize { ns_equality(other) } end |
#[](member) ⇒ Object
Attribute Reference
118 119 120 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 118 def [](member) synchronize { ns_get(member) } end |
#[]=(member, value) ⇒ Object
Attribute Assignment
Sets the value of the given struct member or the member at the given index.
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 185 def []=(member, value) if member.is_a? Integer length = synchronize { @values.length } if member >= length raise IndexError.new("offset #{member} too large for struct(size:#{length})") end synchronize { @values[member] = value } else send("#{member}=", value) end rescue NoMethodError raise NameError.new("no member '#{member}' in struct") end |
#each {|value| ... } ⇒ Object
Yields the value of each struct member in order. If no block is given an enumerator is returned.
139 140 141 142 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 139 def each(&block) return enum_for(:each) unless block_given? synchronize { ns_each(&block) } end |
#each_pair {|member, value| ... } ⇒ Object
Yields the name and value of each struct member in order. If no block is given an enumerator is returned.
152 153 154 155 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 152 def each_pair(&block) return enum_for(:each_pair) unless block_given? synchronize { ns_each_pair(&block) } end |
#inspect ⇒ String Also known as: to_s
Describe the contents of this struct in a string.
72 73 74 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 72 def inspect synchronize { ns_inspect } end |
#merge(other) {|member, selfvalue, othervalue| ... } ⇒ Synchronization::AbstractStruct
Returns a new struct containing the contents of ‘other` and the contents of `self`. If no block is specified, the value for entries with duplicate keys will be that of `other`. Otherwise the value for each duplicate key is determined by calling the block with the key, its value in `self` and its value in `other`.
94 95 96 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 94 def merge(other, &block) synchronize { ns_merge(other, &block) } end |
#select {|value| ... } ⇒ Array
Yields each member value from the struct to the block and returns an Array containing the member values from the struct for which the given block returns a true value (equivalent to ‘Enumerable#select`).
167 168 169 170 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 167 def select(&block) return enum_for(:select) unless block_given? synchronize { ns_select(&block) } end |
#to_h ⇒ Hash
Returns a hash containing the names and values for the struct’s members.
103 104 105 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 103 def to_h synchronize { ns_to_h } end |
#values ⇒ Array Also known as: to_a
Returns the values for this struct as an Array.
51 52 53 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 51 def values synchronize { ns_values } end |
#values_at(*indexes) ⇒ Object
Returns the struct member values for each selector as an Array.
A selector may be either an Integer offset or a Range of offsets (as in ‘Array#values_at`).
63 64 65 |
# File 'lib/concurrent-ruby/concurrent/mutable_struct.rb', line 63 def values_at(*indexes) synchronize { ns_values_at(indexes) } end |