From: Kristof Bastiaensen Date: 2004-04-09T10:34:15+09:00 Subject: Re: why yield does not set $_ ? 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 > 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.. 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