Global variable -- last expression

Hi,

In IRB the last expression is in _ . But if you are in a ruby program
how to get the last expression.

Thanks
Basha

···

--
Posted via http://www.ruby-forum.com/.

$_ ?

You assign it to a variable:

value = 1 + 1

puts "the value was #{value}"

···

On Oct 21, 2011, at 5:34 PM, basha c. wrote:

In IRB the last expression is in _ . But if you are in a ruby program
how to get the last expression.

Steve Klabnik wrote in post #1027862:

$_ ?

It stores the last gets not the last expression return...

···

--
Posted via http://www.ruby-forum.com/\.

Mightn't that lead to some "shallow copy" problems in some cases?

···

On Sat, Oct 22, 2011 at 10:48:00AM +0900, Eric Hodel wrote:

On Oct 21, 2011, at 5:34 PM, basha c. wrote:
> In IRB the last expression is in _ . But if you are in a ruby program
> how to get the last expression.

You assign it to a variable:

value = 1 + 1

puts "the value was #{value}"

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

No more than in IRB.

···

On Oct 21, 2011, at 7:46 PM, Chad Perrin wrote:

On Sat, Oct 22, 2011 at 10:48:00AM +0900, Eric Hodel wrote:

On Oct 21, 2011, at 5:34 PM, basha c. wrote:

In IRB the last expression is in _ . But if you are in a ruby program
how to get the last expression.

You assign it to a variable:

value = 1 + 1

puts "the value was #{value}"

Mightn't that lead to some "shallow copy" problems in some cases?

Eric is right. IRB just sets _ as a variable.

In "irb/context.rb" you'll find:

162: def set_last_value(value)
163: @last_value = value
164: @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
165: end

Take a look at line 164, _ is set right there.

-Dane

···

On Fri, Oct 21, 2011 at 8:05 PM, Eric Hodel <drbrain@segment7.net> wrote:

On Oct 21, 2011, at 7:46 PM, Chad Perrin wrote:

On Sat, Oct 22, 2011 at 10:48:00AM +0900, Eric Hodel wrote:

On Oct 21, 2011, at 5:34 PM, basha c. wrote:

In IRB the last expression is in _ . But if you are in a ruby program
how to get the last expression.

You assign it to a variable:

value = 1 + 1

puts "the value was #{value}"

Mightn't that lead to some "shallow copy" problems in some cases?

No more than in IRB.