class Upgrow::ImmutableStruct

A read-only Struct. An Immutable Struct is initialized with its member values and subsequent state changes are not permitted.

Public Class Methods

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

Creates a new Immutable Struct class with the given members.

@param args [Array<Symbol>] the list of members for the new class.

@return [ImmutableStruct] the new Immutable Struct class able to

accommodate the given members.
Calls superclass method
# File lib/upgrow/immutable_struct.rb, line 14
def new(*args, &block)
  if args.any? { |member| !member.is_a?(Symbol) }
    raise ArgumentError, 'all members must be symbols'
  end

  struct_class = super(*args, keyword_init: true, &block)

  struct_class.members.each do |member|
    struct_class.send(:undef_method, :"#{member}=")
  end

  struct_class
end
new(**args) click to toggle source

Initializes a new Immutable Struct with the given member values.

@param args [Hash<Symbol, Object>] the list of values for each member of

the Immutable Struct.
Calls superclass method
# File lib/upgrow/immutable_struct.rb, line 35
def initialize(**args)
  members.each { |key| args.fetch(key) }
  super(**args)
  freeze
end