Newbie question- Return method

Discussion in 'Ruby' started by James Schoonmaker, Oct 1, 2008.

  1. I'm trying to teach myself the basics of Ruby (based on the guide at
    http://pine.fm/LearnToProgram), and I ran across the Return method. I'm
    not clear on what it does and how it works, though, and all of the
    searches I made for it didn't address the method itself.

    Any help?
    --
    Posted via http://www.ruby-forum.com/.
    James Schoonmaker, Oct 1, 2008
    #1
    1. Advertising

  2. The word "return" is a reserved word in ruby, just like the words
    "def", "if", "while", etc...

    "return" is used in a method to exit the method and to return the
    given value to the caller. In principal, you could write

    def compute_square(x)
    return x*x
    end

    ...although The Ruby Way(tm) would be to simply write:

    def compute_square(x)
    x*x
    end

    since, by the definition of the language, the return value of a method
    is (99.99% of the time) the last expression computed in the method.

    --wpd


    On Wed, Oct 1, 2008 at 8:33 AM, James Schoonmaker
    <> wrote:
    > I'm trying to teach myself the basics of Ruby (based on the guide at
    > http://pine.fm/LearnToProgram), and I ran across the Return method. I'm
    > not clear on what it does and how it works, though, and all of the
    > searches I made for it didn't address the method itself.
    >
    > Any help?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Patrick Doyle, Oct 1, 2008
    #2
    1. Advertising

  3. Patrick Doyle wrote:
    > The word "return" is a reserved word in ruby, just like the words
    > "def", "if", "while", etc...
    >
    > "return" is used in a method to exit the method and to return the
    > given value to the caller. In principal, you could write
    >
    > def compute_square(x)
    > return x*x
    > end
    >
    > ...although The Ruby Way(tm) would be to simply write:
    >
    > def compute_square(x)
    > x*x
    > end
    >
    > since, by the definition of the language, the return value of a method
    > is (99.99% of the time) the last expression computed in the method.
    >


    You can use it to pick what you want returned if it is not the last
    thing in the method.

    e.g.

    def myMethod s
    d = 'foo'
    s.upcase!
    end

    p myMethod("hiya")

    => "HIYA"

    However

    def myMethod s
    d = "foo"
    s.upcase!
    return d
    end

    p myMethod("hiya")

    => "foo"

    --
    Posted via http://www.ruby-forum.com/.
    Lloyd Linklater, Oct 1, 2008
    #3
  4. Lloyd Linklater wrote:
    > def myMethod s
    > =C2=A0 d =3D 'foo'
    > =C2=A0 s.upcase!
    > end
    >
    > p myMethod("hiya")
    >
    > =3D> =C2=A0"HIYA"
    >
    > However
    >
    > def myMethod s
    > =C2=A0 d =3D "foo"
    > =C2=A0 s.upcase!
    > =C2=A0 return d
    > end
    >
    > p myMethod("hiya")


    Yes, but:
    def myMethod s
    d =3D "foo"
    s.upcase!
    d
    end
    p myMethod("hiya")

    Does the same without return.

    =2D-=20
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 1, 2008
    #4
  5. Sebastian Hungerecker wrote:
    > Lloyd Linklater wrote:
    >>
    >> def myMethod s
    >>   d = "foo"
    >>   s.upcase!
    >>   return d
    >> end
    >>
    >> p myMethod("hiya")

    >
    > Yes, but:
    > def myMethod s
    > d = "foo"
    > s.upcase!
    > d
    > end
    > p myMethod("hiya")
    >
    > Does the same without return.


    While that is true, I wanted to keep it to the point for this thread. I
    did refer to it when I said,

    > Lloyd Linklater wrote:
    >>
    >> You can use it to pick what you want returned if it is not the last
    >> thing in the method.

    --
    Posted via http://www.ruby-forum.com/.
    Lloyd Linklater, Oct 1, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Magne Bergfjord
    Replies:
    1
    Views:
    446
  2. Greenhorn
    Replies:
    15
    Views:
    796
    Keith Thompson
    Mar 6, 2005
  3. Shekhar
    Replies:
    0
    Views:
    180
    Shekhar
    Jul 16, 2004
  4. Xeno Campanoli
    Replies:
    0
    Views:
    213
    Xeno Campanoli
    Feb 13, 2006
  5. Roger Pack
    Replies:
    2
    Views:
    85
    Phrogz
    Jan 2, 2010
Loading...

Share This Page