Skip to content

BasicObject#instance_eval: support the String form - #437

Merged
tannevaled merged 1 commit into
mainfrom
feat/cluster10
Aug 24, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/cluster10

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

instance_eval only handled the block form and ignored any arguments,
so the String form — instance_eval("code" [, filename [, lineno]]) —
fell through to the "no block given" LocalJumpError, and every example
that evaluates a string in the receiver's context failed.

The String form now compiles the source and runs it with the receiver
as self and the receiver's singleton class as the definee (so a def
in the source becomes a singleton method and self / instance variables
resolve to the receiver); an immediate receiver, which has no singleton
class, uses its own class as the definee so read-only source still
works. The optional filename argument is coerced via #to_str and
recorded on the compiled sequence (defaulting to the caller's file);
arity is validated (no block and no args, more than three args, and a
block together with arguments are ArgumentErrors), and bad source
raises SyntaxError.

Verified against ruby 4.0.6; core/basicobject/instance_eval_spec goes
7 → 21 with no regressions (a pre-existing test that encoded the old
LocalJumpError for 5.instance_eval is updated to the MRI
ArgumentError). Whole-suite coverage gate stays at 100.0%. The
remaining failures are the deeper lexical-scope, line-number and
immediate-singleton edges, left for follow-up.

🤖 Generated with Claude Code

instance_eval only handled the block form and ignored any arguments, so
the String form — instance_eval("code" [, filename [, lineno]]) — fell
through to the "no block given" LocalJumpError, and every core/main-style
example that evaluates a string in the receiver's context failed.

The String form now compiles the source and runs it with the receiver as
self and the receiver's singleton class as the definee (so a def in the
source becomes a singleton method and self / instance variables resolve
to the receiver); an immediate receiver, which has no singleton class,
uses its own class as the definee so read-only source still works. The
optional filename argument is coerced via #to_str and recorded on the
compiled sequence (defaulting to the caller's file); arity is validated
(no block and no args, or more than three, and a block together with
arguments, are ArgumentErrors), and bad source raises SyntaxError.

Verified against ruby 4.0.6; core/basicobject/instance_eval_spec goes
7 -> 21. (The remaining failures are the deeper lexical-scope,
line-number and immediate-singleton edges, left for follow-up.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 1d1e59f into main Aug 24, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/cluster10 branch August 24, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant