module Concurrent::MutableStruct

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.

@see ruby-doc.org/core/Struct.html Ruby standard library `Struct`

Constants

FACTORY

Private Class Methods

new(*args, &block) click to toggle source

@!macro struct_new

# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 210
def self.new(*args, &block)
  clazz_name = nil
  if args.length == 0
    raise ArgumentError.new('wrong number of arguments (0 for 1+)')
  elsif args.length > 0 && args.first.is_a?(String)
    clazz_name = args.shift
  end
  FACTORY.define_struct(clazz_name, args, &block)
end

Public Instance Methods

==(other) click to toggle source

@!macro struct_equality

Equality

@return [Boolean] true if other has the same struct subclass and has
  equal member values (according to `Object#==`)
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 128
def ==(other)
  synchronize { ns_equality(other) }
end
[](member) click to toggle source

@!macro struct_get

Attribute Reference

@param [Symbol, String, Integer] member the string or symbol name of the member
  for which to obtain the value or the member's index

@return [Object] the value of the given struct member or the member at the given index.

@raise [NameError] if the member does not exist
@raise [IndexError] if the index is out of range.
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 118
def [](member)
  synchronize { ns_get(member) }
end
[]=(member, value) click to toggle source

@!macro struct_set

Attribute Assignment

Sets the value of the given struct member or the member at the given index.

@param [Symbol, String, Integer] member the string or symbol name of the member
  for which to obtain the value or the member's index

@return [Object] the value of the given struct member or the member at the given index.

@raise [NameError] if the name does not exist
@raise [IndexError] if the index is out of range.
# 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(&block) click to toggle source

@!macro struct_each

Yields the value of each struct member in order. If no block is given
an enumerator is returned.

@yield the operation to be performed on each struct member
@yieldparam [Object] value each struct value (in order)
# 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(&block) click to toggle source

@!macro struct_each_pair

Yields the name and value of each struct member in order. If no block is
given an enumerator is returned.

@yield the operation to be performed on each struct member/value pair
@yieldparam [Object] member each struct member (in order)
@yieldparam [Object] value each struct value (in order)
# 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() click to toggle source

@!macro struct_inspect

Describe the contents of this struct in a string.

@return [String] the contents of this struct in a string
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 72
def inspect
  synchronize { ns_inspect }
end
Also aliased as: to_s
merge(other, &block) click to toggle source

@!macro struct_merge

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`.

@param [Hash] other the hash from which to set the new values
@yield an options block for resolving duplicate keys
@yieldparam [String, Symbol] member the name of the member which is duplicated
@yieldparam [Object] selfvalue the value of the member in `self`
@yieldparam [Object] othervalue the value of the member in `other`

@return [Synchronization::AbstractStruct] a new struct with the new values

@raise [ArgumentError] of given a member that is not defined in the struct
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 94
def merge(other, &block)
  synchronize { ns_merge(other, &block) }
end
select(&block) click to toggle source

@!macro struct_select

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`).

@yield the operation to be performed on each struct member
@yieldparam [Object] value each struct value (in order)

@return [Array] an array containing each value for which the block returns true
# 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_a()
Alias for: values
to_h() click to toggle source

@!macro struct_to_h

Returns a hash containing the names and values for the struct’s members.

@return [Hash] the names and values for the struct’s members
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 103
def to_h
  synchronize { ns_to_h }
end
to_s()
Alias for: inspect
values() click to toggle source

@!macro struct_values

Returns the values for this struct as an Array.

@return [Array] the values for this struct
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 51
def values
  synchronize { ns_values }
end
Also aliased as: to_a
values_at(*indexes) click to toggle source

@!macro struct_values_at

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`).

@param [Fixnum, Range] indexes the index(es) from which to obatin the values (in order)
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 63
def values_at(*indexes)
  synchronize { ns_values_at(indexes) }
end

Private Instance Methods

initialize_copy(original) click to toggle source

@!visibility private

Calls superclass method
# File lib/concurrent-ruby/concurrent/mutable_struct.rb, line 202
def initialize_copy(original)
  synchronize do
    super(original)
    ns_initialize_copy
  end
end