Class: SuperStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/superstruct.rb

Overview

SuperStruct is an enhanced version of the Ruby Standard library Struct.

Compared with the original version, it provides the following additional features:

  • ability to initialize an instance from Hash

  • ability to pass a block on creation

Instance Method Summary collapse

Constructor Details

#initialize({ Symbol = > Object }) ⇒ SuperStruct #initialize([ value1, value1, ... ]) ⇒ SuperStruct #initialize(value1, value1, ...) ⇒ SuperStruct

Overwrites the standard Struct initializer to add the ability to create an instance from a Hash of parameters.

Examples:

attributes = { :foo => 1, :bar => "baz" }
Struct.new(attributes)
# => #<Struct foo=1, bar="baz">

Overloads:

  • #initialize({ Symbol = > Object }) ⇒ SuperStruct

    Initializes the object with a key/value hash.

    Parameters:

    • values ({ Symbol => Object })
  • #initialize([ value1, value1, ... ]) ⇒ SuperStruct

    Initializes the object with given values.

    Parameters:

    • values (Array)
  • #initialize(value1, value1, ...) ⇒ SuperStruct

    Initializes the object with given values.

Yields:

  • self



34
35
36
37
38
39
40
41
42
43
# File 'lib/superstruct.rb', line 34

def initialize(*args)
  if args.first.is_a? Hash
    initialize_with_hash(args.first)
  elsif args.size == 0
    super
  else
    raise ArgumentError
  end
  yield(self) if block_given?
end