[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to set $_ in a method?

Heesob Park

6/12/2009 7:46:00 AM

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 Heesob

6 Answers

Bertram Scharpf

6/12/2009 11:18:00 AM

0

Hi,

Am Freitag, 12. Jun 2009, 16:45:51 +0900 schrieb Heesob Park:
> 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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Daniel Berger

6/12/2009 1:55:00 PM

0



> -----Original Message-----
> From: Bertram Scharpf [mailto:lists@bertram-scharpf.de]
> Sent: Friday, June 12, 2009 5:18 AM
> To: ruby-talk ML
> Subject: Re: How to set $_ in a method?
>
> Hi,
>
> Am Freitag, 12. Jun 2009, 16:45:51 +0900 schrieb Heesob Park:
> > 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).

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


Lord Valve

10/21/2011 2:53:00 PM

0

Ray wrote:

> I think that post says more about the loser who posted it than it does about Berry and his impact on music. I shudder to think what sort of sick shit that bucket of filth gets up to in his spare time, given the spew he feels compelled to share with the world.

You have a problem with the truth, obviously.

And here you are, dissing a truthteller and supporting
a pervert. And that makes you, um...what? A creep.

Bye-bye, creep. Have a Really Nice Day - if you can.


Got guns?

Lord Valve
American - so far


Lord Valve

10/21/2011 2:57:00 PM

0

frschoonover wrote:

> On Oct 20, 10:45 am, Lord Valve <detri...@ix.netcom.com> wrote:
> >
> > Recreation. ;-)
> >
> > Lord Valve
> > Musician
>
> There are other things that one can do for recreation. Collecting
> vinyl records as a hobby is a form of recreation, for example. There
> are people who collect comic books, sports cards, old music magazines,
> pictures of stars, and even extra curricular activities like picnics
> and games played by people at the picnics as a form of recreation. The
> boundaries are endless.
>
> Something to think about.Sincerely Frank R.Schoonover

Why, yes, of course, of course.

Once could make (and collect) shit/piss videos; one
could install cameras in ladies bathrooms, screw underage
girls, jack people, look into people's windows, stuff like
that. All perfectly acceptible forms of recreation for a "musician."

Indeed, I do understand. Thanks for your input.


Lord Valve
"Hobbyist"/Musician




Lord Valve

10/22/2011 2:17:00 AM

0

frschoonover wrote:

> On Oct 21, 10:57 am, Lord Valve <detri...@ix.netcom.com> wrote:
> >
> > Why, yes, of course, of course.
> >
> > Once could make (and collect) shit/piss videos; one
> > could install cameras in ladies bathrooms, screw underage
> > girls, jack people, look into people's windows, stuff like
> > that. All perfectly acceptible forms of recreation for a "musician."
> >
> > Indeed, I do understand. Thanks for your input.
> >
> > Lord Valve
> > "Hobbyist"/Musician- Hide quoted text -
> >
> > - Show quoted text -
>
> None of us in this group care about what Chuck Berry did/does in his
> personal life.

Obviously. Sickness and perversion is just fine with you.

> What he did/does in his personal life is his business
> and nobody elses.

Horse-shit. He's a criminal, that makes it EVERYBODY'S business.

> We are talking about his music,

Which is ridiculously bad...

> his songs

He only has one song, and it sucks major ass.

> and his talent,

His talent lies in criminality and perversion.

> NOT his personal lifestyle. There are other sorts of groups on
> the internet that talk about tabloid trash and gossip, but this is NOT
> one of them.

Really? Coulda fooled me.

> If you can't contribute anything positive to this group, please don't
> post at all.

Fuck you and the code you rode in on, junior.

Who the fuck died and made you boss, eh? Don't like
it? Tough shit. Complain to my ISP, write a letter to your
congressman, shit or go blind, no-one besides YOU
gives a damn.

> I mean that out of the utmost respect.

Sure you do. Fuck you with a rake. Respectfully.

> Thanks.

No trouble at all.

> Sincerely

Indeed.

>Frank R.Schoonover


Lord Valve
Musician






Lord Valve

10/23/2011 4:48:00 PM

0

frschoonover wrote:

> On Oct 23, 12:02 pm, Lord Valve <detri...@ix.netcom.com> wrote:
> >
> > You are a fucking pervert. <shrug>
> >
> > Got guns?
> >
> > Lord Valve
> > American - so far
> >
>
> I have only one thing to say to you regarding this:
>
> "SPEAK FOR YOURSELF!"
>
> Doesn't say very much, doesn't it?Sincerely Frank R.Schoonover

Hey, bro, looks like yer Freud slipped up on ya.

Double negative, anyone? ROFLMBFAO!

Fuck you , pervert. Sincerely.


Got guns?

Lord Valve
American - so far