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
18 changes: 10 additions & 8 deletions internal/vm/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,11 +1401,8 @@ func (vm *VM) bootstrap() {
}
vm.cSymbol.define("succ", symSucc)
vm.cSymbol.define("next", symSucc)
symIndex := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
return stringIndex(symStr(self), args) // [] / slice yield a String, like MRI
}
vm.cSymbol.define("[]", symIndex)
vm.cSymbol.define("slice", symIndex)
// Symbol#[] / #slice run the whole String#[] protocol against the symbol's
// name (they are registered after strIndexFn is defined, below).
vm.cSymbol.define("start_with?", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value {
s := symStr(self)
for _, a := range args {
Expand Down Expand Up @@ -1900,6 +1897,14 @@ func (vm *VM) bootstrap() {
return res
}
vm.cString.define("[]", strIndexFn)
// Symbol#[] / #slice yield a String by running the full String#[] protocol
// (Integer/Range/String/Regexp with capture groups, setting $~) against the
// symbol's name, exactly like sym.to_s[...].
symIndexFn := func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value {
return strIndexFn(vm, object.NewString(symStr(self)), args, blk)
}
vm.cSymbol.define("[]", symIndexFn)
vm.cSymbol.define("slice", symIndexFn)
// #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 {
Expand Down Expand Up @@ -5524,9 +5529,6 @@ func parseLeadingFloat(s string) float64 {

// stringIndex implements String#[]: s[i], s[i, len], and s[range], all
// rune-indexed, returning nil for an out-of-range start.
func stringIndex(s string, args []object.Value) object.Value {
return stringIndexEnc(s, args, false)
}

// stringIndexEnc implements String#[] / #slice indexing. When binary is true the
// receiver is ASCII-8BIT, so the integer index/length forms count BYTES (not
Expand Down
36 changes: 36 additions & 0 deletions internal/vm/symbol_index_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestSymbolIndex covers Symbol#[] / Symbol#slice running the full String#[]
// protocol against the symbol's name: an Integer index, an index and length, a
// Range, a substring, and a Regexp (with and without a capture index, setting
// $~), each yielding a String or nil. Verified against ruby 4.0.6.
func TestSymbolIndex(t *testing.T) {
cases := []struct{ src, want string }{
{`p :hello[1]`, `"e"`},
{`p :hello[-1]`, `"o"`},
{`p :hello[1, 3]`, `"ell"`},
{`p :hello[1..3]`, `"ell"`},
{`p :hello["ell"]`, `"ell"`},
{`p :hello["xyz"]`, `nil`},
{`p :hello[10]`, `nil`},
// Regexp forms.
{`p :hello[/l+/]`, `"ll"`},
{`p :hello[/l(l)/, 1]`, `"l"`},
{`p :hello[/xyz/]`, `nil`},
// A Regexp match sets $~.
{`:hello[/l+/]; p $~[0]`, `"ll"`},
// #slice is the same.
{`p :hello.slice(1, 3)`, `"ell"`},
}
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")
}
}
}