Commit 7df75f7a

7df75f7a1fb9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Implement Struct#to_h

Person.new("Alice", 30).to_h was unresolved. Build a symbol-keyed hash mapping each member name to its value, boxed by inferred type, into a sym_poly_hash (members can be heterogeneous). The member-name symbols come from the same interned table the struct definition populated.

Like to_a/values, gated on a synthetic struct without its own to_h.

Fixes #1238

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
7df75f7a1fb9
Parent
6f513a0b5956

spinel_analyze.rbmodifiedView file

@@ -5583,10 +5583,15 @@
55835583 # `to_h` on a Hash variant is identity. Surface so the caller
55845584 # gets the recv's exact type rather than an unresolved fallback.
55855585 # Struct member accessors: `S.new(...).to_a` / `.values` collect the
5586- # member values into a poly_array (members can be heterogeneous).
5587- if mname == "to_a" || mname == "values"
5586+ # member values into a poly_array (members can be heterogeneous);
5587+ # `.to_h` builds a symbol-keyed hash of member name => value.
5588+ if mname == "to_a" || mname == "values" || mname == "to_h"
55885589 sci_sv = struct_recv_ci(recv)
55895590 if sci_sv >= 0 && cls_find_method(sci_sv, mname) < 0
5591+ if mname == "to_h"
5592+ @needs_sym_poly_hash = 1
5593+ return "sym_poly_hash"
5594+ end
55905595 @needs_poly_array = 1
55915596 return "poly_array"
55925597 end