Skip to content

$~ after String#sub with a block ignores a match made inside the block #137

Closed

Description

@Largo

After String#sub with a block, $~ should be whatever the block's frame last set. A match made inside the block is overwritten by sub's own match:

def m; "ab".sub(/(.)/) { "x".match(/(.)/); "z" }; p $1; end
m
# CRuby: "x"   IronRuby: "a"

Found by running the scripts PR #119 deleted (Tests/Experiments, Tests/Legacy) on CRuby 4.0.9 and on IronRuby built from master c7f70cf (net10.0, Linux). (Experiments/Strings/sub_scan_regex.rb)

Activity

  1. added a commit that references this issue on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions