to_h()
Returns a Hash containing the names and values for the struct’s members.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_h[:address] #=> "123 Maple, Anytown NC"
static VALUE rb_struct_to_h(VALUE s) { VALUE h = rb_hash_new(); VALUE members = rb_struct_members(s); long i; for (i=0; i<RSTRUCT_LEN(s); i++) { rb_hash_aset(h, rb_ary_entry(members, i), RSTRUCT_GET(s, i)); } return h; }