How can I use the last returned value?

Hello!

In Python, I use '_'(underscore) for the last returned value.

print "Hello"

"Hello"

x = _
print x

"Hello"

What is the equivalent in Ruby?

Thanks in advance.

Sam

If you mean what's the equivalent in irb then my answer would be:

IRB.CurrentContext.last_value

But that's a bit cumbersome :wink:
To get easier access to the last value, I put the following into my irb.rc:

def ans
   IRB.CurrentContext.last_value
end

....and then can simply use it like this:

irb(main):001:0> 2+2
=> 4
irb(main):002:0> ans*2
=> 8

hth.

···

On Tue, 08 Jun 2004 16:53:39 GMT, Sam Sungshik Kong <ssk@chol.nospam.net> wrote:

In Python, I use '_'(underscore) for the last returned value.

print "Hello"

"Hello"

x = _
print x

"Hello"

What is the equivalent in Ruby?

--
exoticorn/farbrausch

Dennis Ranke wrote:

To get easier access to the last value, I put the following into my irb.rc:

def ans
  IRB.CurrentContext.last_value
end

Nice. I tried defining the same with the name _, but that fails because _ happens to be a local variable that's already defined in an irb session. Oh, well.

irb(main):001:0> defined?(_)
=> "local-variable"
irb(main):002:0> local_variables
=> ["_"]
irb(main):003:0> def _
irb(main):004:1> IRB.CurrentContext.last_value
irb(main):005:1> end
=> nil
irb(main):006:0> 3+4
=> 7
irb(main):007:0> _
=> nil

Hi,

At Wed, 9 Jun 2004 02:03:37 +0900,
Dennis Ranke wrote in [ruby-talk:102807]:

> In Python, I use '_'(underscore) for the last returned value.
>>>> print "Hello"
> "Hello"
>>>> x = _
>>>> print x
> "Hello"
>
> What is the equivalent in Ruby?

If you mean what's the equivalent in irb then my answer would be:

IRB.CurrentContext.last_value

But that's a bit cumbersome :wink:

EVAL_HISTORY enables _.

$ grep HISTORY ~/.irbrc
IRB.conf[:EVAL_HISTORY] = 1000
IRB.conf[:SAVE_HISTORY] = 100

$ irb
irb(main):001:0> 2+2
=> 4
irb(main):002:0> _
=> 4
irb(main):003:0>

···

--
Nobu Nakada

Hi!

Thanks for the reply.

However, I cannot do it.
Could you elaborate a little bit more?

I use Windows XP.
And I brought up 'irb' and tried the following:

irb(main):001:0> irb.conf[:EVAL_HISTORY]=1000
irb#1(main):001:0> irb.conf[:SAVE_HISTORY]=100
irb#2(main):001:0> 3+3
=> 6
irb#2(main):002:0> _
=> nil
irb#2(main):003:0>

What's wrong?

Thanks again.

Sam
<nobu.nokada@softhome.net> wrote in message
news:200406082253.i58MrAwl019111@sharui.nakada.niregi.kanuma.tochigi.jp...

···

Hi,

At Wed, 9 Jun 2004 02:03:37 +0900,
Dennis Ranke wrote in [ruby-talk:102807]:
> > In Python, I use '_'(underscore) for the last returned value.
> >>>> print "Hello"
> > "Hello"
> >>>> x = _
> >>>> print x
> > "Hello"
> >
> > What is the equivalent in Ruby?
>
> If you mean what's the equivalent in irb then my answer would be:
>
> IRB.CurrentContext.last_value
>
> But that's a bit cumbersome :wink:

EVAL_HISTORY enables _.

$ grep HISTORY ~/.irbrc
IRB.conf[:EVAL_HISTORY] = 1000
IRB.conf[:SAVE_HISTORY] = 100

$ irb
irb(main):001:0> 2+2
=> 4
irb(main):002:0> _
=> 4
irb(main):003:0>

--
Nobu Nakada

Hi,

At Wed, 9 Jun 2004 09:38:38 +0900,
Sam Sungshik Kong wrote in [ruby-talk:102876]:

irb(main):001:0> irb.conf[:EVAL_HISTORY]=1000
irb#1(main):001:0> irb.conf[:SAVE_HISTORY]=100

You have to put those lines in ~/.irbrc, .irbrc, irbrc, _irbrc
or $irbrc file.

···

--
Nobu Nakada

Sam Sungshik Kong wrote in [ruby-talk:102876]:
> irb(main):001:0> irb.conf[:EVAL_HISTORY]=1000
> irb#1(main):001:0> irb.conf[:SAVE_HISTORY]=100

You have to put those lines in ~/.irbrc, .irbrc, irbrc, _irbrc
or $irbrc file.

--
Nobu Nakada

To Sam Sungshik Kong,

I just made this work for the first time by putting the lines in:

    C:\ruby\lib\ruby\site_ruby\irb.rc

Always felt weird having files beginning with a dot on Windows.

daz

Wow, it works!

Thanks daz and Nobu Nokada.
You're the men.

Sam
"daz" <dooby@d10.karoo.co.uk> wrote in message
news:huCcnUg8JMZ9PFvdSa8jmw@karoo.co.uk...

···

> Sam Sungshik Kong wrote in [ruby-talk:102876]:
> > irb(main):001:0> irb.conf[:EVAL_HISTORY]=1000
> > irb#1(main):001:0> irb.conf[:SAVE_HISTORY]=100
>
> You have to put those lines in ~/.irbrc, .irbrc, irbrc, _irbrc
> or $irbrc file.
>
> --
> Nobu Nakada
>
>

To Sam Sungshik Kong,

I just made this work for the first time by putting the lines in:

    C:\ruby\lib\ruby\site_ruby\irb.rc

Always felt weird having files beginning with a dot on Windows.

daz