Storing a block and calling it later wanting it to return put of the method it's called in.

Discussion in 'Ruby' started by Trans, Aug 9, 2005.

  1. Trans

    Trans Guest

    class D
    def initialize(&blk)
    @blk = blk
    end

    def lambda_return
    lambda { return 'Y' }.call ; 'N'
    end

    def proc_return
    Proc.new { return 'Y' }.call ; 'N'
    end

    def lambda_return_blk
    lambda(&@blk).call ; 'N'
    end

    def proc_return_blk
    Proc.new(&@blk).call ; 'N'
    end

    end

    d = D.new { return 'Y' }

    p d.lambda_return
    p d.proc_return

    p d.lambda_return_blk
    p d.proc_return_blk

    _produces_

    "N"
    "Y"
    "N"
    LocalJumpError

    Anyway to get the last to return the "Y"?

    Thanks,
    T.
     
    Trans, Aug 9, 2005
    #1
    1. Advertising

  2. Hi,

    At Tue, 9 Aug 2005 11:36:09 +0900,
    Trans wrote in [ruby-talk:151317]:
    >
    > class D
    > def initialize(&blk)
    > @blk = blk
    > end
    >
    > def lambda_return
    > lambda { return 'Y' }.call ; 'N'
    > end
    >
    > def proc_return
    > Proc.new { return 'Y' }.call ; 'N'
    > end
    >
    > def lambda_return_blk
    > lambda(&@blk).call ; 'N'
    > end
    >
    > def proc_return_blk

    Proc.new(&@blk).call
    rescue LocalJumpError => e
    raise unless e.backtrace.size == caller.size+2
    e.exit_value
    else
    'N'
    > end
    >
    > end


    --
    Nobu Nakada
     
    nobuyoshi nakada, Aug 9, 2005
    #2
    1. Advertising

  3. Re: Storing a block and calling it later wanting it to return putof the method it's called in.

    Trans wrote:

    > d = D.new { return 'Y' }
    >
    > p d.lambda_return
    > p d.proc_return
    >
    > p d.lambda_return_blk
    > p d.proc_return_blk
    >
    >_produces_
    >
    > "N"
    > "Y"
    > "N"
    > LocalJumpError
    >
    >Anyway to get the last to return the "Y"?
    >
    >

    -1 +1
    -d = D.new { return 'Y' }
    +d = D.new { 'Y' }

    IIRC, PickAxe has a decent section on the difference between Proc.new,
    lambda, and blocks, specifically pertaining to 'return'.

    Devin
     
    Devin Mullins, Aug 9, 2005
    #3
  4. Re: Storing a block and calling it later wanting it to return putof the method it's called in.

    Trans wrote:

    >Anyway to get the last to return the "Y"?
    >
    >

    Except I'm an idiot. Nevermind. The short answer is 'no', but maybe it
    should be 'you're not supposed to do that'.

    Devin
     
    Devin Mullins, Aug 9, 2005
    #4
  5. Trans

    Trans Guest

    nobuyoshi nakada wrote:
    > >
    > > def proc_return_blk

    > Proc.new(&@blk).call
    > rescue LocalJumpError => e
    > raise unless e.backtrace.size == caller.size+2
    > e.exit_value
    > else
    > 'N'
    > > end
    > >
    > > end


    That clarifies some things for me. Thanks.

    T.
     
    Trans, Aug 9, 2005
    #5
  6. Trans

    Trans Guest

    local/non-local return of a Function class (was Storing a block and calling it later...)

    Devin Mullins wrote:
    > -1 +1
    > -d = D.new { return 'Y' }
    > +d = D.new { 'Y' }
    >
    > IIRC, PickAxe has a decent section on the difference between Proc.new,
    > lambda, and blocks, specifically pertaining to 'return'.


    Yea, I need to read that over. I'm starting to get a better picture...

    I'm trying to create an abstract Funtion class that can sort-of
    delegate itself as a block, lambda, Proc or method. So one of of things
    involved is setting a flag to tell it whether to return locally or not.
    So for example instead of my original two methods:

    def lambda_return
    lambda { return 'Y' }.call ; 'N'
    end

    def proc_return
    Proc.new { return 'Y' }.call ; 'N'
    end

    I'd put:

    def lambda_return
    Function.new { return 'Y' }.local.call ; 'N'
    end

    def proc_return
    Function.new { return 'Y' }.nonlocal.call ; 'N'
    end

    again expecting

    'N'
    'Y'

    I _may_ have worked it out. But I'm not for sure. I need to run it
    through some paces.

    T.
     
    Trans, Aug 9, 2005
    #6
    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:
    473
  2. Kevin

    Storing URLs for later lookup

    Kevin, May 25, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    366
    Steven Cheng[MSFT]
    May 30, 2007
  3. storing error dumps for later persual

    , Sep 2, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    127
    peter kelcey
    Sep 11, 2005
  4. Jacob Repp
    Replies:
    0
    Views:
    83
    Jacob Repp
    Feb 24, 2006
  5. Replies:
    5
    Views:
    133
Loading...

Share This Page