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

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.

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

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

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

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.

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.