How can I use the last returned value?

  • Thread starter Sam Sungshik Kong
  • Start date
S

Sam Sungshik Kong

Hello!

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

What is the equivalent in Ruby?

Thanks in advance.

Sam
 
D

Dennis Ranke

In Python, I use '_'(underscore) for the last returned value.
"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 ;)
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.
 
J

Joel VanderWerf

Dennis said:
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
 
N

nobu.nokada

Hi,

At Wed, 9 Jun 2004 02:03:37 +0900,
Dennis Ranke wrote in [ruby-talk:102807]:
If you mean what's the equivalent in irb then my answer would be:

IRB.CurrentContext.last_value

But that's a bit cumbersome ;)

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>
 
S

Sam Sungshik Kong

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
Hi,

At Wed, 9 Jun 2004 02:03:37 +0900,
Dennis Ranke wrote in [ruby-talk:102807]:
If you mean what's the equivalent in irb then my answer would be:

IRB.CurrentContext.last_value

But that's a bit cumbersome ;)

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>
 
N

nobu.nokada

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.
 
D

daz

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top