Module: Concurrent::SettableStruct

Defined in:

lib/concurrent/settable_struct.rb

Overview

An thread-safe, write-once variation of Ruby's standard Struct.
Each member can have its value set at most once, either at construction
or any time thereafter. Attempting to assign a value to a member
that has already been set will result in a Concurrent::ImmutabilityError.

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

The first two forms are used to create a new struct subclass class_name
that can contain a value for each member_name . This subclass can be
used to create instances of the structure like any other Class .

If the class_name is omitted an anonymous struct class will be created.
Otherwise, the name of this struct will appear as a constant in the struct class,
so it must be unique for all structs under this base class and must start with a
capital letter. Assigning a struct class to a constant also gives the class
the name of the constant.

If a block is given it will be evaluated in the context of StructClass, passing
the created class as a parameter. This is the recommended way to customize a struct.
Subclassing an anonymous struct creates an extra anonymous class that will never be used.

The last two forms create a new instance of a struct subclass. The number of value
parameters must be less than or equal to the number of attributes defined for the
struct. Unset parameters default to nil. Passing more parameters than number of attributes
will raise an ArgumentError.

# File 'lib/concurrent/settable_struct.rb', line 95defself.new(*args,&block)clazz_name=nilifargs.length==0raiseArgumentError.new('wrong number of arguments (0 for 1+)')elsifargs.length>0&&args.first.is_a?(String)clazz_name=args.shiftendFACTORY.define_struct(clazz_name,args,&block)end

Instance Method Details

#==(other) ⇒ Boolean

Equality

Returns:

(Boolean)
—

true if other has the same struct subclass and has
equal member values (according to Object#==)

# File 'lib/concurrent/settable_struct.rb', line 75def[]=(member,value)ifmember.is_a?Integerlength=synchronize{@values.length}ifmember>=lengthraiseIndexError.new("offset #{member} too large for struct(size:#{length})")endsynchronizedounless@values[member].nil?raiseConcurrent::ImmutabilityError.new('struct member has already been set')end@values[member]=valueendelsesend("#{member}=",value)endrescueNoMethodErrorraiseNameError.new("no member '#{member}' in struct")end

#each {|value| ... } ⇒ undocumented

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

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.

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