DID YOU KNOW? -- break can return a value?

Discussion in 'Ruby' started by trans. (T. Onoma), Oct 18, 2004.

  1. An interesting tidbit I pickup today. Did you know that break can return a
    value from it's block?

    a = loop do
    break (1 + 1)
    end

    puts a #=> 4


    T.
    --
    ( o _ カラãƒ
    // trans.
    / \

    I don't give a damn for a man that can only spell a word one way.
    -Mark Twain
     
    trans. (T. Onoma), Oct 18, 2004
    #1
    1. Advertising

  2. trans.  (T. Onoma)

    Dave Burt Guest

    "trans. (T. Onoma)" <> wrote...
    > An interesting tidbit I pickup today. Did you know that break can return a
    > value from it's block?
    >
    > a = loop do
    > break (1 + 1)
    > end
    >
    > puts a #=> 4
    >


    What platform gives that counterintuitive result? *grins*
     
    Dave Burt, Oct 18, 2004
    #2
    1. Advertising

  3. trans.  (T. Onoma)

    zuzu Guest

    On Mon, 18 Oct 2004 11:49:23 +0900, Dave Burt <> wrote:
    > "trans. (T. Onoma)" <> wrote...
    > > An interesting tidbit I pickup today. Did you know that break can return a
    > > value from it's block?
    > >
    > > a = loop do
    > > break (1 + 1)
    > > end
    > >
    > > puts a #=> 4
    > >

    >
    > What platform gives that counterintuitive result? *grins*


    by defining the variable "a" with code "loop", it is a Proc object, yes?

    (sorry, i would test this myself except i'm away from my ruby console.)

    (if so, i must simultaneously pronounce my dislike for imperative loop
    structures, and my praise for ruby perceiving all code as in blocks /
    procedures (functional/subroutine style).)

    also, quite right dave, what gives with the "trailing off the edge of
    a procedure 'return' " producing a "value" of 4? perhaps that's the
    point; what exactly is going on when passing arguments into the break
    function/keyword?

    peace,
    -z
     
    zuzu, Oct 18, 2004
    #3
  4. On 2004-10-18 21:39:40 +0900, zuzu wrote:
    > also, quite right dave, what gives with the "trailing off the edge of
    > a procedure 'return' " producing a "value" of 4? perhaps that's the
    > point; what exactly is going on when passing arguments into the break
    > function/keyword?


    Whatever it may be, I am quite sure 1 + 1 is still equal to 2.

    --
    Florian Frank
     
    Florian Frank, Oct 18, 2004
    #4
  5. On Monday 18 October 2004 09:10 am, Florian Frank wrote:
    | On 2004-10-18 21:39:40 +0900, zuzu wrote:
    | > also, quite right dave, what gives with the "trailing off the edge of
    | > a procedure 'return' " producing a "value" of 4? perhaps that's the
    | > point; what exactly is going on when passing arguments into the break
    | > function/keyword?
    |
    | Whatever it may be, I am quite sure 1 + 1 is still equal to 2.

    :) Yes, that's a typo, a would be 2, not 4. Strange how easy that is to
    overlook!

    T.
     
    trans. (T. Onoma), Oct 18, 2004
    #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. Vijay Kumar R Zanvar

    Did you know?

    Vijay Kumar R Zanvar, Sep 29, 2003, in forum: C Programming
    Replies:
    47
    Views:
    1,247
    Joona I Palaste
    Oct 4, 2003
  2. pek
    Replies:
    0
    Views:
    1,219
  3. Daniel Waite
    Replies:
    2
    Views:
    230
    Daniel Waite
    May 2, 2008
  4. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    242
    Gregory Toomey
    Apr 23, 2004
  5. Mark Lawrence
    Replies:
    63
    Views:
    209
    Robert Kern
    May 17, 2014
Loading...

Share This Page