Return

Hello,

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

Peace be upon you –

Junayeed Ahnaf Nirjhor

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

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 Wed, Oct 19, 2011 at 02:26:54AM +0900, Junayeed Ahnaf Nirjhor wrote:

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

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 10/18/2011 01:26 PM, Junayeed Ahnaf Nirjhor wrote:

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

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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

···

On Tue, Oct 18, 2011 at 6:26 PM, Junayeed Ahnaf Nirjhor < zombiegenerator@aol.com> wrote:

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

--
http://richardconroy.blogspot.com | http://twitter.com/RichardConroy

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

···

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce <mcpierce@gmail.com> wrote:

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

--
Phillip Gawlowski

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

Fair enough.

···

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

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce <mcpierce@gmail.com> 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:

--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

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

···

On 19/10/11 08:32, Phillip Gawlowski wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce<mcpierce@gmail.com> 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.

Sam Duncan wrote in post #1027273:

···

On 19/10/11 08:32, Phillip Gawlowski wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce<mcpierce@gmail.com> 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.

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

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

···

On Tue, Oct 18, 2011 at 10:26 PM, Sam Duncan <sduncan@wetafx.co.nz> wrote:

On 19/10/11 08:32, Phillip Gawlowski wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce<mcpierce@gmail.com> >> 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"

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Indeed ;]

···

On 19/10/11 11:13, Brian Candler wrote:

Sam Duncan wrote in post #1027273:

On 19/10/11 08:32, Phillip Gawlowski wrote:

On Tue, Oct 18, 2011 at 8:29 PM, Darryl L. Pierce<mcpierce@gmail.com> 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.