Repository navigation
BasicObject#instance_eval: support the String form - #437
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
instance_evalonly 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 examplethat evaluates a string in the receiver's context failed.
The String form now compiles the source and runs it with the receiver
as
selfand the receiver's singleton class as the definee (so adefin the source becomes a singleton method and
self/ instance variablesresolve 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_strandrecorded 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 sourceraises
SyntaxError.Verified against ruby 4.0.6;
core/basicobject/instance_eval_specgoes7 → 21 with no regressions (a pre-existing test that encoded the old
LocalJumpErrorfor5.instance_evalis updated to the MRIArgumentError). Whole-suite coverage gate stays at 100.0%. Theremaining failures are the deeper lexical-scope, line-number and
immediate-singleton edges, left for follow-up.
🤖 Generated with Claude Code