Module: Modern::Struct::Copy

Included in:
Modern::Struct
Defined in:
lib/modern/struct.rb

Instance Method Summary collapse

Instance Method Details

#copy(fields = {}) ⇒ Object

This implementation is necessary because the “fast” way (hash, merge, recreate) WILL EAT YOUR TYPE DATA. This is the only way I can find to copy-but-change an object that doesn’t.

Computers are bad.



16
17
18
# File 'lib/modern/struct.rb', line 16

def copy(fields = {})
  self.class[self.class.attribute_names.map { |n| [n, self[n]] }.to_h.merge(fields)]
end