Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Struct#deconstruct_keys

(::Array[::Struct::index & ::Hash::_Key]? indices) -> ::Hash[::Struct::index & ::Hash::_Key, E]
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 #[]= #each also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0