# How to set $\_ in a method?

**URL:** <https://www.ruby-forum.com/t/how-to-set-in-a-method/170101>\
**Category:** Ruby\
**Created:** [June 12, 2009, 9:46am UTC](https://www.ruby-forum.com/t/how-to-set-in-a-method/170101 "2009-06-12T09:46:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phasis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e56c9b/32.png) [@phasis](https://www.ruby-forum.com/u/phasis)\
**Post date:** [June 12, 2009, 9:46am UTC](https://www.ruby-forum.com/t/how-to-set-in-a-method/170101/1 "2009-06-12T09:46:31Z")

</div>

Hi all,

I like to set caller’s $\_ variable in a method, but it did’t work for  
me.  
Consider this:

def foo  
puts "$\_ = #{$_.inspect} enter foo"  
$_ = ‘def’  
puts “$\_ = #{$\_.inspect} exit foo”  
end

$\_ = ‘abc’  
puts “$\_ = #{$_.inspect} before foo"  
foo  
puts "$_ = #{$\_.inspect} after foo”

The result is:  
$\_ = “abc” before foo  
$\_ = nil enter foo  
$\_ = “def” exit foo  
$\_ = “abc” after foo

I noticed $\_ is is local to the current scope.  
Is it impossible to set the caller’s $\_ variable in a method without  
using binding and eval?

Thanks,

Park H.

---

<div class="post-metadata">

**Author:** ![phasis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e56c9b/32.png) [@phasis](https://www.ruby-forum.com/u/phasis)\
**Post date:** [June 12, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/how-to-set-in-a-method/170101/2 "2009-06-12T13:19:05Z")

</div>

Hi,

Am Freitag, 12. Jun 2009, 16:45:51 +0900 schrieb Heesob P.:

> I like to set caller’s $\_ variable in a method, but it did’t work for me.  
> […]  
> I noticed $\_ is is local to the current scope.  
> Is it impossible to set the caller’s $\_ variable in a method without  
> using binding and eval?

$\_ has some special meanings in Kernel.gets, Kernel.sub, Regexp#~  
etc. This comes in handy for -ne/-pe option oneliners. You  
shouldn’t use it outside that (if you don’t know what you do).

Bertram

---

<div class="post-metadata">

**Author:** ![phasis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e56c9b/32.png) [@phasis](https://www.ruby-forum.com/u/phasis)\
**Post date:** [June 17, 2009, 1:34am UTC](https://www.ruby-forum.com/t/how-to-set-in-a-method/170101/3 "2009-06-17T01:34:50Z")

</div>

On Fri, Jun 12, 2009 at 8:55 AM, Daniel B.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The rb\_lastline\_set() call sets $\_. What’s the equivalent in pure Ruby? If  
> it’s possible to do within a C extension, it ought to be possible to do in  
> pure Ruby.

It is not possible, which is why backref and lastline should be  
banned. $\_ and $~ are treated specially and can only be set across  
calls from C code (or Java code in JRuby).

- Charlie

---

<div class="post-metadata">

**Author:** ![phasis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e56c9b/32.png) [@phasis](https://www.ruby-forum.com/u/phasis)\
**Post date:** [June 12, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/how-to-set-in-a-method/170101/4 "2009-06-12T15:55:45Z")

</div>

> me.
> 
> > […]  
> > I noticed $\_ is is local to the current scope.  
> > Is it impossible to set the caller’s $\_ variable in a method without  
> > using binding and eval?
> 
> $\_ has some special meanings in Kernel.gets, Kernel.sub, Regexp#~  
> etc. This comes in handy for -ne/-pe option oneliners. You  
> shouldn’t use it outside that (if you don’t know what you do).

Except we’re trying to match the zlib.c spec. Specifically,  
rb\_gzreader\_gets() in zlib.c:

static VALUE  
rb\_gzreader\_gets(argc, argv, obj)  
int argc;  
VALUE \*argv;  
VALUE obj;  
{  
VALUE dst;  
dst = gzreader\_gets(argc, argv, obj);  
if (!NIL\_P(dst)) {  
rb\_lastline\_set(dst);  
}  
return dst;  
}

The rb\_lastline\_set() call sets $\_. What’s the equivalent in pure Ruby?  
If  
it’s possible to do within a C extension, it ought to be possible to do  
in  
pure Ruby.

Regards,

Dan
