Why yield does not set $_?

Hi,

Probably I just need to sleep, but I wonder: why $_ from yield?
It would be useful for iterators’ loops.

I’d expect this to work:
file.each { gsub /a/,‘b’}
(using Kernel#gsub)

but it does not. Are there performance reasons or something else for
this?

Notice that, in general, I don’t like $_ and hope it disappears, but
while it is still there I’m someway seduced from it…

PS <not_so_semiserious>
On this line, it would be even interesting to default every method
called withouth a receiver to $_

[1,2,3].map { .+ 1} #=> [2,3,4]

We could even kill Kernel::gsub, ::split and the likes as we can
achieve similar result with just a ‘.’ more…

or:
[1,2,3].any? { .==1}

BTW, I suppose ruby would become perl this way…
</not_so_semiserious>

Hello,
as far as I know, $_ has nothing to do with method invocation.
$_ gets only set after using gets or readline to the line read.
Maybe you are confusing with perl?
Kernel::gsub makes indeed use of $, as a special case.
In the case of
file.each { gsub /a/,‘b’}
$
will not be set, that’s why this will not work.

Greetings,
Kristof

···

On Fri, 19 Mar 2004 13:40:45 +0100, gabriele renzi @ google wrote:

Hi,

Probably I just need to sleep, but I wonder: why $_ from yield? It would
be useful for iterators’ loops.

I’d expect this to work:
file.each { gsub /a/,‘b’}
(using Kernel#gsub)

but it does not. Are there performance reasons or something else for
this?

Notice that, in general, I don’t like $_ and hope it disappears, but
while it is still there I’m someway seduced from it…

PS <not_so_semiserious>
On this line, it would be even interesting to default every method
called withouth a receiver to $_

[1,2,3].map { .+ 1} #=> [2,3,4]

We could even kill Kernel::gsub, ::split and the likes as we can achieve
similar result with just a ‘.’ more…

or:
[1,2,3].any? { .==1}

BTW, I suppose ruby would become perl this way… </not_so_semiserious>