return in blocks

Discussion in 'Ruby' started by Lyndon Samson, Nov 20, 2005.

  1. Whats the official word on using return to return a value in a block,
    rather than just the value of the last expression?

    Using ruby 1.8.2 (2004-12-25) [i386-mswin32] I see strange behaviour (
    silent crash ) when explicitly using 'return' to return from a block,
    yet otherwise the code runs fine.

    running under ntsd

    eax=3D74fd5054 ebx=3D7ffdf000 ecx=3D0241cdac edx=3D00000000 esi=3D77f8dd80 =
    edi=3D00000000
    eip=3D77f8dd8b esp=3D0240fe2c ebp=3D0240fef4 iopl=3D0 nv up ei pl z=
    r na po nc
    cs=3D001b ss=3D0023 ds=3D0023 es=3D0023 fs=3D0038 gs=3D0000 =
    efl=3D00000246
    ntdll!NtTerminateProcess+b:
    77f8dd8b c20800 ret 0x8

    Which I can't interpret as anything in particular.


    --
    Into RFID? www.rfidnewsupdate.com Simple, fast, news.
     
    Lyndon Samson, Nov 20, 2005
    #1
    1. Advertising

  2. Hi,

    In message "Re: return in blocks"
    on Sun, 20 Nov 2005 19:29:40 +0900, Lyndon Samson <> writes:

    |Whats the official word on using return to return a value in a block,
    |rather than just the value of the last expression?

    Usually use "break <value>" to pass a value from a block.

    |Using ruby 1.8.2 (2004-12-25) [i386-mswin32] I see strange behaviour (
    |silent crash ) when explicitly using 'return' to return from a block,
    |yet otherwise the code runs fine.

    Show us a concrete code to reproduce your crash, please, preferably
    using more recent version, at least 1.8.3.

    matz.
     
    Yukihiro Matsumoto, Nov 20, 2005
    #2
    1. Advertising

  3. Lyndon Samson

    Lionel Thiry Guest

    Lyndon Samson a écrit :
    > Whats the official word on using return to return a value in a block,
    > rather than just the value of the last expression?


    return in a block doesn't just return the block, it returns the method
    where the block has been created.

    Exemple:

    def my_method
    test = ["one", "two", "three"]
    test.each do |e|
    if e == "three"
    return "my_method returned"
    end
    puts e
    end
    puts "will it ever goes here?"
    end

    puts my_method

    output:

    one
    two
    my_method_returned


    --
    Lionel Thiry

    Personal web site: http://users.skynet.be/lthiry/
     
    Lionel Thiry, Nov 21, 2005
    #3
  4. ------=_Part_30258_5705931.1132546603178
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 11/21/05, Lionel Thiry <> wrote:
    >
    > Lyndon Samson a =E9crit :
    > > Whats the official word on using return to return a value in a block,
    > > rather than just the value of the last expression?

    >
    > return in a block doesn't just return the block, it returns the method
    > where the block has been created.
    >
    > Hence my silence, I'm sure when I check this will turn out to be what

    happened.

    Apologies for the noise :)

    ------=_Part_30258_5705931.1132546603178--
     
    Lyndon Samson, Nov 21, 2005
    #4
    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. PvdK
    Replies:
    0
    Views:
    2,990
  2. Arjen
    Replies:
    3
    Views:
    454
    Scott Allen
    Feb 27, 2005
  3. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  4. matt
    Replies:
    1
    Views:
    285
    George Ogata
    Aug 6, 2004
  5. Steven Taylor
    Replies:
    9
    Views:
    271
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page