Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Storing a block and calling it later wanting it to return put of the method it's called in.

Reply
Thread Tools

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

 
 
Trans
Guest
Posts: n/a
 
      08-09-2005

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.

 
Reply With Quote
 
 
 
 
nobuyoshi nakada
Guest
Posts: n/a
 
      08-09-2005
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


 
Reply With Quote
 
 
 
 
Devin Mullins
Guest
Posts: n/a
 
      08-09-2005
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



 
Reply With Quote
 
Devin Mullins
Guest
Posts: n/a
 
      08-09-2005
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



 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      08-09-2005

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.

 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      08-09-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
storing values for later use progcat@comcast.net Ruby 5 06-11-2008 02:47 AM
Storing URLs for later lookup Kevin ASP .Net 3 05-30-2007 11:28 AM
Question: is it possible for an extension to convert a block to a proc and execute it later Jacob Repp Ruby 0 02-24-2006 07:33 AM
storing error dumps for later persual apandapion@gmail.com ASP .Net Web Services 1 09-11-2005 02:38 AM
Problem: return from one method when another method is called Magne Bergfjord Java 1 04-20-2004 04:25 PM



Advertisments