From ccbbc6e72da439acb34e42859f8ee0ab2edb72b7 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 28 Aug 2026 14:15:45 +0200 Subject: [PATCH] Add Symbol#casecmp, #casecmp?, #match and #match? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Symbol gained the last of its String-delegating query methods: - casecmp / casecmp? compare symbol names case-insensitively, but only against another Symbol — any other argument yields nil, as MRI does (a String is never coerced here). - match / match? run a Regexp or String pattern against the symbol name by delegating to String, so #match still sets $~. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 15880 -> 15890 (core/symbol casecmp +8, match +5). Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 23 ++++++++++++++++++++++ internal/vm/symbol_index_internal_test.go | 24 +++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 3c1080bc..d4f0dced 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1956,6 +1956,29 @@ func (vm *VM) bootstrap() { } vm.cSymbol.define("[]", symIndexFn) vm.cSymbol.define("slice", symIndexFn) + // casecmp / casecmp? compare symbol names case-insensitively, but only against + // another Symbol — any other argument yields nil, as MRI does (String is never + // coerced here). They delegate to the String comparison of the two names. + symCasecmp := func(meth string) NativeFn { + return func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + other, ok := args[0].(object.Symbol) + if !ok { + return object.NilV + } + return vm.send(object.NewString(symStr(self)), meth, + []object.Value{object.NewString(string(other))}, nil) + } + } + vm.cSymbol.define("casecmp", symCasecmp("casecmp")) + vm.cSymbol.define("casecmp?", symCasecmp("casecmp?")) + // match / match? run the pattern (a Regexp or String) against the symbol name + // by delegating to String, so #match still sets $~. + vm.cSymbol.define("match", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + return vm.send(object.NewString(symStr(self)), "match", args, blk) + }) + vm.cSymbol.define("match?", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + return vm.send(object.NewString(symStr(self)), "match?", args, blk) + }) // #slice is a true alias of #[] (shares the exact method record). aliasBuiltin(vm.cString, "slice", "[]") vm.cString.define("ord", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { diff --git a/internal/vm/symbol_index_internal_test.go b/internal/vm/symbol_index_internal_test.go index b40d98f3..f9d36291 100644 --- a/internal/vm/symbol_index_internal_test.go +++ b/internal/vm/symbol_index_internal_test.go @@ -34,3 +34,27 @@ func TestSymbolIndex(t *testing.T) { } } } + +// TestSymbolCasecmpMatch covers Symbol#casecmp / #casecmp? (Symbol-only, +// case-insensitive name comparison) and #match / #match? (delegating to the name +// string, with #match setting $~). Asserted against MRI Ruby 4.0.6. +func TestSymbolCasecmpMatch(t *testing.T) { + cases := []struct{ src, want string }{ + {`p :Hello.casecmp(:hELLO)`, "0"}, + {`p :abc.casecmp(:abd)`, "-1"}, + {`p :abc.casecmp("ABC")`, "nil"}, // a String argument is not compared + {`p :abc.casecmp(1)`, "nil"}, + {`p :Hello.casecmp?(:hELLO)`, "true"}, + {`p :abc.casecmp?("ABC")`, "nil"}, + {`p :hello.match?(/l+/)`, "true"}, + {`p :hello.match?("z")`, "false"}, + {`p :hello.match(/l+/).class`, "MatchData"}, + {`:hello.match(/l+/); p $~[0]`, `"ll"`}, + {`p :hello.match(/z/)`, "nil"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 1575884a..8dde623d 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15880 +15890