Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Struct#deconstruct_keys

deconstruct_keys(array_of_names) → hash

Returns a hash of the name/value pairs for the given member names.

Example
Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
h = joe.deconstruct_keys([:zip, :address])
h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}

Returns all names and values if array_of_names is nil:

Example
h = joe.deconstruct_keys(nil)
h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
See also #deconstruct #dig #as_json #each also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0