Yukihiro Matsumoto wrote:

> Hi,

Moin!

> In message "Re: Bug when rerouting String#gsub with a block using $1?"
>     on 03/08/18, Florian Gross <flgr / ccan.de> writes:
> 
> |IMHO this is a confusing 
> |trap and thus a source of unnecessary debugging sessions for users.
> 
> Then don't use ugly dollar variables.

I'm not using them myself which is the problem. irb uses them and I
don't really want to break irb. So unfortunately not using them is no
solution in this case, because others will use them as long as they
are there.

> But perhaps gsub should have passed the match data to the block for
> convenience.

I agree. Currently there's no other way than using $~ in the block of
a gsub do get more MatchData than $~[0]. I think that you could just
replace the current behavior (passing $~[0]) so that $~ gets passed.
The only case where this would be incompatible is fixed by giving the
passed value #to_str as a synonym for #to_s.

> |That aside: Are there other methods like sub, sub!, gsub and gsub! 
> |which have this special behavior?
> 
> "gets" modifies $_ in local scope.  $_ and $~ (and $1 etc) are treated
> specially.

Ah, thanks, I don't think I'll have to special case gets because it
doesn't take a block however which is a Good Thing.

So this problem is really just about sub, sub!, gsub and gsub!. Would
changing those cases so that the $1 vars are taken out of the scope,
that belongs to the closure, be hardly possible at the implementation
level or are there other reasons for not changing this?

Thanks for your attention!

> matz.

Regards,
Florian Gross