Method: Concurrent::ImmutableStruct.new
- Defined in:
- lib/concurrent/immutable_struct.rb
.new(*args, &block) ⇒ Object
Factory for creating new struct classes.
“‘ new([class_name] [, member_name]+>) -> StructClass click to toggle source new([class_name] [, member_name]+>) {|StructClass| block } -> StructClass new(value, …) -> obj StructClass[value, …] -> obj “`
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.
74 75 76 77 78 79 80 81 82 |
# File 'lib/concurrent/immutable_struct.rb', line 74 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 |