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

T

Trans

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.
 
N

nobuyoshi nakada

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'
 
D

Devin Mullins

Trans said:
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
 
D

Devin Mullins

Trans said:
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
 
T

Trans

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

That clarifies some things for me. Thanks.

T.
 
T

Trans

Devin said:
-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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top