Class: Struct::Initializer::Definition

Inherits:
Module
  • Object
show all
Defined in:
lib/struct/initializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(names, keyword_init: false) ⇒ Definition

Returns a new instance of Definition.



8
9
10
11
12
13
14
15
16
17
# File 'lib/struct/initializer.rb', line 8

def initialize(names, keyword_init: false)
  attr_reader(*names)
  arguments = names.map { "#{_1}#{":" if keyword_init}" }.join(", ")

  class_eval <<~RUBY, __FILE__, __LINE__ + 1
    def initialize(#{arguments})
      #{names.map { "@#{_1} = #{_1}" }.join("\n")}
    end
  RUBY
end