Professional Web Applications Themes

return from yielded block - Ruby

i saw something like this recently on clr but can't seem to find it ... what i want to do is something like value = meth 42 where def meth n block_meth do if n == 42 return 'forty-two' else return 'ask another question' end end end def block_meth yield end the problem is that you can't 'return' from a block - so how is this done? -a -- (short)...

  1. #1

    Default return from yielded block


    i saw something like this recently on clr but can't seem to find it ...

    what i want to do is something like

    value = meth 42

    where

    def meth n
    block_meth do
    if n == 42
    return 'forty-two'
    else
    return 'ask another question'
    end
    end
    end

    def block_meth
    yield
    end


    the problem is that you can't 'return' from a block - so how is this done?

    -a
    --
    (short)

    Ara.T.Howard Guest

  2. #2

    Default Re: return from yielded block

    Hi,

    At Fri, 13 Feb 2004 14:54:56 +0900,
    Ara.T.Howard wrote in [ruby-talk:92771]: 

    Return to where? The next statement of yield?
    If so, you may want "next".

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  3. #3

    Default Re: return from yielded block

    il Thu, 12 Feb 2004 22:40:12 -0700, "Ara.T.Howard"
    <gov> ha scritto::
     


    this works..

    irb(main):001:0> def meth n
    irb(main):002:1> block_meth do
    irb(main):003:2* if n == 42
    irb(main):004:3> return 'forty-two'
    irb(main):005:3> else
    irb(main):006:3* return 'ask another question'
    irb(main):007:3> end
    irb(main):008:2> end
    irb(main):009:1> end
    => nil
    irb(main):010:0>
    irb(main):011:0* def block_meth
    irb(main):012:1> yield
    irb(main):013:1> end
    => nil
    irb(main):014:0> meth 10
    => "ask another question"
    irb(main):015:0> meth 42
    => "forty-two"


    Maybe you wan "next" ?
    gabriele Guest

  4. #4

    Default Re: return from yielded block

    Hi Ara,

    "Ara.T.Howard" <gov> wrote in message news:<ngdc.noaa.gov>...
    [..] 

    I think the return is returning from the yielding procedure rather
    than the block, as the following demonstrates:

    def meth n
    block_meth do
    if n == 42
    return 'forty-two'
    end
    end
    end

    def block_meth
    yield
    return "ask another question"
    end

    puts meth(42)
    puts meth(40)
    Ged Guest

  5. #5

    Default Re: return from yielded block

    On Fri, 13 Feb 2004 net wrote:
     
    >
    > Return to where? The next statement of yield?
    > If so, you may want "next".[/ref]

    return a value. i was not clear enough, let me try again:

    def meth s
    x =
    block_meth do
    if s =~ /forty/
    return 40
    else
    return 0
    end
    end
    x + 2
    end

    def block_meth; yield; end

    p(meth('forty')) # this prints 40 - i want it to print 42


    -a

    Ara.T.Howard Guest

  6. #6

    Default Re: return from yielded block

    Hi,

    At Sat, 14 Feb 2004 00:54:55 +0900,
    Ara.T.Howard wrote in [ruby-talk:92793]: 

    Use break or next instead of return.

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  7. #7

    Default Re: return from yielded block


    "Ara.T.Howard" <ngdc.noaa.gov> schrieb im Newsbeitrag
    news:ngdc.noaa.gov... [/ref][/ref]
    done? 
    >
    > return a value. i was not clear enough, let me try again:
    >
    > def meth s
    > x =
    > block_meth do
    > if s =~ /forty/
    > return 40
    > else
    > return 0
    > end
    > end
    > x + 2
    > end
    >
    > def block_meth; yield; end
    >
    > p(meth('forty')) # this prints 40 - i want it to print 42[/ref]

    Simply delete the "return"'s:

    irb(main):001:0> def meth s
    irb(main):002:1> x =
    irb(main):003:1* block_meth do
    irb(main):004:2* if s =~ /forty/
    irb(main):005:3> 40
    irb(main):006:3> else
    irb(main):007:3* 0
    irb(main):008:3> end
    irb(main):009:2> end
    irb(main):010:1> x + 2
    irb(main):011:1> end
    => nil
    irb(main):012:0>
    irb(main):013:0* def block_meth; yield; end
    => nil
    irb(main):014:0>
    irb(main):015:0* p(meth('forty')) # this prints 40 - i want it to print 42
    42
    => nil
    irb(main):016:0>

    I guess you are aware of the fact that this kind of construction is a bit,
    err, complicated and your real world code does a lot more in between...
    :-)

    Regards

    robert

    Robert Guest

  8. #8

    Default Re: return from yielded block

    On Sat, 14 Feb 2004 net wrote:
     
    >
    > Use break or next instead of return.[/ref]

    thanks! thought i tried that - guess not. i like the look of

    break 40

    but not

    next 40

    are the semantics _exactly_ the same?

    -a
    --

    ATTN: please update your address books with address below!

    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | STP :: http://www.ngdc.noaa.gov/stp/
    | NGDC :: http://www.ngdc.noaa.gov/
    | NESDIS :: http://www.nesdis.noaa.gov/
    | NOAA :: http://www.noaa.gov/
    | US DOC :: http://www.commerce.gov/
    |
    | The difference between art and science is that science is what we
    | understand well enough to explain to a computer.
    | Art is everything else.
    | -- Donald Knuth, "Discover"
    |
    | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
    ================================================== =============================

    Ara.T.Howard Guest

  9. #9

    Default Re: return from yielded block

    On Fri, 13 Feb 2004, Robert Klemme wrote:
     [/ref]
    > done? 
    > >
    > > return a value. i was not clear enough, let me try again:
    > >
    > > def meth s
    > > x =
    > > block_meth do
    > > if s =~ /forty/
    > > return 40
    > > else
    > > return 0
    > > end
    > > end
    > > x + 2
    > > end
    > >
    > > def block_meth; yield; end
    > >
    > > p(meth('forty')) # this prints 40 - i want it to print 42[/ref]
    >
    > Simply delete the "return"'s:
    >
    > irb(main):001:0> def meth s
    > irb(main):002:1> x =
    > irb(main):003:1* block_meth do
    > irb(main):004:2* if s =~ /forty/
    > irb(main):005:3> 40
    > irb(main):006:3> else
    > irb(main):007:3* 0
    > irb(main):008:3> end
    > irb(main):009:2> end
    > irb(main):010:1> x + 2
    > irb(main):011:1> end
    > => nil
    > irb(main):012:0>
    > irb(main):013:0* def block_meth; yield; end
    > => nil
    > irb(main):014:0>
    > irb(main):015:0* p(meth('forty')) # this prints 40 - i want it to print 42
    > 42
    > => nil
    > irb(main):016:0>
    >
    > I guess you are aware of the fact that this kind of construction is a bit,
    > err, complicated and your real world code does a lot more in between... :-)[/ref]

    yes. and yes - about 1700 lines to be exact ;-)

    nobu's reply was the answer - your response works here but i also had a bunch
    of stuff after the if/else/end : it was not the default return value. for
    some reason i just didn't explain myself well enough - sorry. the bottom line
    is that i needed a way to prematurely 'return' a value from a yielded block
    under $DEBUG/$NOOP conditions for testing...

    cheers.

    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================


    Ara.T.Howard Guest

  10. #10

    Default Re: return from yielded block

    "Ara.T.Howard" <ngdc.noaa.gov> wrote in message news:<ngdc.noaa.gov>...
     

    The problem is that "return" returns from the enclosing method. The
    code works as you want it to if you take out the "return" keywords,
    like this:

    def meth s
    x =
    block_meth do
    if s =~ /forty/
    40
    else
    0
    end
    end
    x + 2
    end

    def block_meth; yield; end

    p(meth('forty')) # this prints 42
    Karl Guest

  11. #11

    Default Re: return from yielded block

    Hi,

    In message "Re: return from yielded block"
    on 04/02/14, "Ara.T.Howard" <ngdc.noaa.gov> writes:

    |> > def meth s
    |> > x =
    |> > block_meth do
    |> > if s =~ /forty/
    |> > return 40
    |> > else
    |> > return 0
    |> > end
    |> > end
    |> > x + 2
    |> > end
    |> >
    |> > def block_meth; yield; end
    |> >
    |> > p(meth('forty')) # this prints 40 - i want it to print 42
    |>
    |> Use break or next instead of return.

    |are the semantics _exactly_ the same?

    No. "break" terminates "block_meth", "next" terminates "yield".

    matz.


    Yukihiro Guest

Similar Threads

  1. IP Block
    By Med Marijuana in forum Macromedia Dynamic HTML
    Replies: 7
    Last Post: April 30th, 05:33 PM
  2. goToNetPage, return or not return
    By GONS in forum Macromedia Director Basics
    Replies: 1
    Last Post: January 16th, 08:44 PM
  3. block specified ip
    By R C P in forum Web Design
    Replies: 1
    Last Post: December 26th, 02:21 PM
  4. Replies: 0
    Last Post: January 8th, 08:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139