# using a predefined hash hash = { field1: "foo", field2: "bar" } HashStruct = Struct.new(*hash.keys, keyword_init: true) hash_struct = HashStruct.new(hash) hash_struct.field1 # => "foo" hash_struct.field2 # => "bar" hash_struct.field3 # => ERROR! # using an array ary = [:field1, :field2] AryStruct = Struct.new(*ary, keyword_init: true) ary_hash = {} ary_hash[:field1] = "foo" ary_hash[:field2] = "bar" ary_struct = AryStruct.new(ary_hash) ary_struct.field1 # => "foo" ary_struct.field2 # => "bar" ary_struct.field3 # => ERROR!