Return

Hello,

What does return do? I mean, it returns something, then why does it vary
from puts?

Peace be upon you –

Junayeed Ahnaf N.

Twitter - @Nirjhor http://twitter.com/nirjhor

return is how you pass a value from a method back to the caller of the
method

puts is a builtin that sends the value(s) given to stdout

not really related in my mind

cheers

On Wed, Oct 19, 2011 at 02:26:54AM +0900, Junayeed Ahnaf N. wrote:

What does return do? I mean, it returns something, then why does it vary from
puts?

(1+1)

That expression returns a value of 2.

foo = (1+1)

That captures the return value of 2 in a variable called foo.

puts (1+1)

That prints the return value of 2 as a string to standard output, and
adds a newline to the end of it.

foo = (1+1)

That, too, is an expression with a return value. The return value
happens to be the same value captured in the variable foo, so once again
the return value of 2 applies.

puts (1+1)

The puts method has a return value of nil, so that expression returns a
value of nil.

foo = puts (1+1)

Here, the nil return value of the puts method is captured in the foo
variable, which then returns a value of nil.

If you use irb, it looks a bit like this:

$ irb
>> (1+1)
=> 2
>> foo = (1+1)
=> 2
>> puts (1+1)
2
=> nil
>> foo = puts (1+1)
2
=> nil

Those lines that start with >> are the prompts where I entered Ruby
expressions.

The lines that start with => show the return values of those expressions
when they are executed.

The lines that show nothing but 2 are the lines that show what puts
sends
to standard output.

Here’s a little more irb experimentation on the subject:

>> puts puts (1+1)
2

=> nil
>> puts (puts (1+1)).inspect
2
nil
=> nil

I hope that helps.

On 10/18/2011 01:26 PM, Junayeed Ahnaf N. wrote:

What does return do? I mean, it returns something, then why does it vary from
puts?

The goal of puts is to output text to the console.

The goal of return is the exit the current method and return to one
frame lower in the call stack.

They’re two totally unrelated things.

On Tue, Oct 18, 2011 at 6:26 PM, Junayeed Ahnaf N. <
[email protected]> wrote:

Hello,
What does return do? I mean, it returns something, then why does it vary
from puts?

Try: http://ruby-kickstart.com/ - excellent resource for learning Ruby.
Explains a lot of these basic questions.

On 10/18/2011 03:32 PM, Phillip G. wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce [email protected] wrote:

The goal of puts is to output text to the console.

To be technical: It provides output to the OS’s standard output
facility. Which may or may not be a console. :slight_smile:

Fair enough.

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce [email protected]
wrote:

The goal of puts is to output text to the console.

To be technical: It provides output to the OS’s standard output
facility. Which may or may not be a console. :slight_smile:


Phillip G.

gplus.to/phgaw | twitter.com/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

On 19/10/11 08:32, Phillip G. wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce[email protected] wrote:

The goal of puts is to output text to the console.
To be technical: It provides output to the OS’s standard output
facility.

$stdout = File.new(’/dev/null’, ‘w’)
puts “ohai, I am my OS’s standard output facility O_o”

On 19/10/11 11:13, Brian C. wrote:

Rather, it sends it to the $stdout object, which is initialised to an IO
object wrapping the OS’s standard output, but may be changed to point to
a different IO object.

Indeed ;]

Sam D. wrote in post #1027273:

On 19/10/11 08:32, Phillip G. wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce[email protected] wrote:

The goal of puts is to output text to the console.
To be technical: It provides output to the OS’s standard output
facility.

$stdout = File.new(’/dev/null’, ‘w’)
puts “ohai, I am my OS’s standard output facility O_o”

Which actually goes to show: puts doesn’t send output to the OS’s
standard output facility.

Rather, it sends it to the $stdout object, which is initialised to an IO
object wrapping the OS’s standard output, but may be changed to point to
a different IO object.

On Tue, Oct 18, 2011 at 10:26 PM, Sam D. [email protected]
wrote:

$stdout = File.new(’/dev/null’, ‘w’)
puts “ohai, I am my OS’s standard output facility O_o”

Usually redirection is done via:

$stdout.reopen("/dev/null")

The reason is, that you simply exchange the reference of $stdout but
you do not modify the underlying file descriptor. As soon as you
invoke another process you will see the output again.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs