Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/vm/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ func setupStruct(vm *VM) {
names[i] = nm
}
sub := vm.newStructClass(base, names, kwInit)
// keyword_init? is a class method of each minted subclass (never of Struct
// itself): true / false when keyword_init: was given, nil when it was omitted.
sub.smethods["keyword_init?"] = &Method{name: "keyword_init?", owner: sub,
native: func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value {
switch structDefOf(self.(*RClass)).kwInit {
case kwTrue:
return object.True
case kwFalse:
return object.False
default:
return object.NilV
}
}}
if haveName && name != "" {
if !validConstName(name) {
raise("NameError", "identifier %s needs to be constant", name)
Expand Down
17 changes: 17 additions & 0 deletions internal/vm/struct_kwinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,20 @@ func TestStructKeywordInit(t *testing.T) {
}
}
}

// TestStructKeywordInitPredicate covers Struct#keyword_init? — the tri-state
// class method (nil when omitted, true / false when given), inherited by a
// subclass. Asserted against MRI Ruby 4.0.6.
func TestStructKeywordInitPredicate(t *testing.T) {
cases := []struct{ src, want string }{
{`p Struct.new(:a, :b).keyword_init?`, "nil\n"},
{`p Struct.new(:a, keyword_init: true).keyword_init?`, "true\n"},
{`p Struct.new(:a, keyword_init: false).keyword_init?`, "false\n"},
{`class KSub < Struct.new(:a, keyword_init: true); end; p KSub.keyword_init?`, "true\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15840
15850