[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Return from a block?

Howard Lewis Ship

1/5/2005 11:12:00 PM

I've had a couple of places where I really needed to just return from
a block. From what I can see, if my block is inside a method, the
return will return from the method, not return control *to* the
method. I've ended up writing some very Pascal like code:

Find.find(*ARGV) do |f|

if f =~ /[CVS|SVN]/
Find.prune
else
$matches += 1 if match?(f)
end
end

Where what I'd prefer to write would be:

Find.find(*ARGV) do |f|

if f =~ /[CVS|SVN]/
Find.prune
return-from-block-to-method
end

$matches += 1 if match?(f)
end


Is there a way to return control from the block to the method?

--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind

Professional Tapestry training, mentoring, support
and project work. http://howardlew...


17 Answers

Carlos

1/5/2005 11:17:00 PM

0

[Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
> Is there a way to return control from the block to the method?

next

Good luck.


Jamis Buck

1/5/2005 11:35:00 PM

0

On 08:17 Thu 06 Jan , Carlos wrote:
> [Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
> > Is there a way to return control from the block to the method?
>
> next
>
> Good luck.

To elaborate a little more:

a = proc { |i| next i+1 }
p a.call(5)

Hope that helps. I can understand why 'return' behaves like it does,
but there are times I wish there was a more intuitive keyword for
returning a value from a block. 'next' just feels wrong to me. :(

- Jamis

--
Jamis Buck
jamis_buck@byu.edu
http://www.jamisbuck...
------------------------------
"I am Victor of Borge. You will be assimil-nine-ed."



Howard Lewis Ship

1/5/2005 11:59:00 PM

0

Thanks ... there it is, plain as day, on page 345 of PickAxe 2nd edition.


On Thu, 6 Jan 2005 08:35:14 +0900, Jamis Buck <jamis_buck@byu.edu> wrote:
> On 08:17 Thu 06 Jan , Carlos wrote:
> > [Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
> > > Is there a way to return control from the block to the method?
> >
> > next
> >
> > Good luck.
>
> To elaborate a little more:
>
> a = proc { |i| next i+1 }
> p a.call(5)
>
> Hope that helps. I can understand why 'return' behaves like it does,
> but there are times I wish there was a more intuitive keyword for
> returning a value from a block. 'next' just feels wrong to me. :(
>
> - Jamis
>
> --
> Jamis Buck
> jamis_buck@byu.edu
> http://www.jamisbuck...
> ------------------------------
> "I am Victor of Borge. You will be assimil-nine-ed."
>
>


--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind

Professional Tapestry training, mentoring, support
and project work. http://howardlew...


Bertram Scharpf

1/6/2005 9:17:00 AM

0

Hi,

Am Donnerstag, 06. Jan 2005, 08:59:06 +0900 schrieb Howard Lewis Ship:
> On Thu, 6 Jan 2005 08:35:14 +0900, Jamis Buck <jamis_buck@byu.edu> wrote:
> >
> > a = proc { |i| next i+1 }
> > p a.call(5)
> >
> Thanks ... there it is, plain as day, on page 345 of PickAxe 2nd edition.

Sorry, where are the page numbers in the online version?

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


Dave Thomas

1/6/2005 9:33:00 AM

0


On Jan 6, 2005, at 3:17 AM, Bertram Scharpf wrote:

>> Thanks ... there it is, plain as day, on page 345 of PickAxe 2nd
>> edition.
>
> Sorry, where are the page numbers in the online version?
>
If you're looking at it online, it's probably the first edition.

Cheers


Dave



Robert Klemme

1/6/2005 12:39:00 PM

0


"Carlos" <angus@quovadis.com.ar> schrieb im Newsbeitrag
news:20050105231702.GA4600@quovadis.com.ar...
> [Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
>> Is there a way to return control from the block to the method?
>
> next

"break "could work, too.

Kind regards

robert

Bertram Scharpf

1/6/2005 1:58:00 PM

0

Am Donnerstag, 06. Jan 2005, 18:32:52 +0900 schrieb Dave Thomas:
>
> On Jan 6, 2005, at 3:17 AM, Bertram Scharpf wrote:
>
> >>Thanks ... there it is, plain as day, on page 345 of PickAxe 2nd
> >>edition.
> >
> >Sorry, where are the page numbers in the online version?
> >
> If you're looking at it online, it's probably the first edition.

What a surprise.

Anyway, I would have liked to know, what 'next <exp>' means.
Never mind; in the meantime I found out.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


e

1/6/2005 3:39:00 PM

0

Jamis Buck wrote:
> On 08:17 Thu 06 Jan , Carlos wrote:
>
>>[Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
>>
>>>Is there a way to return control from the block to the method?
>>
>>next
>>
>>Good luck.
>
>
> To elaborate a little more:
>
> a = proc { |i| next i+1 }
> p a.call(5)
>
> Hope that helps. I can understand why 'return' behaves like it does,
> but there are times I wish there was a more intuitive keyword for
> returning a value from a block. 'next' just feels wrong to me. :(

For just returning control, throw...catch should work, for returning
a value, Kernel.callcc? But Mr. Buck's solution is certainly shorter.

E



Gennady

1/6/2005 3:47:00 PM

0

E S wrote:
> Jamis Buck wrote:
>
>> On 08:17 Thu 06 Jan , Carlos wrote:
>>
>>> [Howard Lewis Ship <hlship@gmail.com>, 2005-01-06 00.11 CET]
>>>
>>>> Is there a way to return control from the block to the method?
>>>
>>>
>>> next
>>>
>>> Good luck.
>>
>>
>>
>> To elaborate a little more:
>>
>> a = proc { |i| next i+1 }
>> p a.call(5)
>>
>> Hope that helps. I can understand why 'return' behaves like it does,
>> but there are times I wish there was a more intuitive keyword for
>> returning a value from a block. 'next' just feels wrong to me. :(
>
>
> For just returning control, throw...catch should work, for returning
> a value, Kernel.callcc? But Mr. Buck's solution is certainly shorter.

throw...catch can be used for returning a value as well, by the way. A
second parameter to "throw" is used as a return value from "catch".

Gennady.

>
> E
>
>



Sea&Gull

1/6/2005 4:37:00 PM

0

Bertram Scharpf wrote:
> Am Donnerstag, 06. Jan 2005, 18:32:52 +0900 schrieb Dave Thomas:
>
>>On Jan 6, 2005, at 3:17 AM, Bertram Scharpf wrote:
>>
>>
>>>>Thanks ... there it is, plain as day, on page 345 of PickAxe 2nd
>>>>edition.
>>>
>>>Sorry, where are the page numbers in the online version?
>>>
>>
>>If you're looking at it online, it's probably the first edition.
>
>
> What a surprise.
>
> Anyway, I would have liked to know, what 'next <exp>' means.
> Never mind; in the meantime I found out.

This C code may illustrate how "next", "break", "redo" behave
in Ruby:

/*****************************/
while (condition) {
label_redo:

goto label_next; /* next */
goto label_break; /* break */
goto label_redo; /* redo */

/* some code */

label_next:

}

label_break:

/*****************************/


--
s&g