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
struct.c
View on GitHub ↗
static VALUE
rb_struct_deconstruct_keys(VALUE s, VALUE keys)
{
return deconstruct_keys(s, keys, false);
}
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}