[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how exit a block?

Greg Willits

8/14/2008 8:21:00 PM

I found a suggestion that says to use return, but this generates a
syntax error

colors = ['red', 'yellow', 'green', 'blue']

colors.each do |color|
p color
return color if color == 'yellow'
end

My actual code is a little different but I am assuming whatever
technique solves the above would solve what I need as well.

Seems like a simple syntax need, but I'm not finding a conclusive
solution. Probably right in front of me, so sorry if it's major
obvious...

-- gw
--
Posted via http://www.ruby-....

5 Answers

Stefano Crocco

8/14/2008 8:23:00 PM

0

On Thursday 14 August 2008, Greg Willits wrote:
> I found a suggestion that says to use return, but this generates a
> syntax error
>
> colors = ['red', 'yellow', 'green', 'blue']
>
> colors.each do |color|
> p color
> return color if color == 'yellow'
> end
>
> My actual code is a little different but I am assuming whatever
> technique solves the above would solve what I need as well.
>
> Seems like a simple syntax need, but I'm not finding a conclusive
> solution. Probably right in front of me, so sorry if it's major
> obvious...
>
> -- gw

break color if color == 'yellow'

Stefano

Joshua Ballanco

8/15/2008 1:10:00 AM

0

Greg Willits wrote:
> I found a suggestion that says to use return, but this generates a
> syntax error

Using 'return' will break you out of a method. If you want to use it to
break out of a loop, wrap your loop in a method. i.e.:

#!/usr/bin/ruby

@colors = %w(red blue yellow green)
def find_yellow
@colors.each do |color|
p color
return color if color == 'yellow'
end
end

puts "I found #{find_yellow}"

HTH,

Josh
--
Posted via http://www.ruby-....

Greg Willits

8/15/2008 4:07:00 AM

0

Stefano Crocco wrote:
> On Thursday 14 August 2008, Greg Willits wrote:
>> My actual code is a little different but I am assuming whatever
>> technique solves the above would solve what I need as well.
>>
>> Seems like a simple syntax need, but I'm not finding a conclusive
>> solution. Probably right in front of me, so sorry if it's major
>> obvious...
>>
>> -- gw
>
> break color if color == 'yellow'


Oi. yep, break is what I was looking for. I guess I've just never come
across it anywhere (and googling anything ruby with "exit block" turns
up 40 billion exit function discussions).

Thx

-- gw

--
Posted via http://www.ruby-....

Joshua Abbott

8/15/2008 4:36:00 AM

0

Have you tried using detect?

colors = ['red', 'yellow', 'green', 'blue']
yellow = colors.detect { |color| color == 'yellow' }

Detect runs the block on each element until the first that returns true.

>> colors = ['red', 'yellow', 'green', 'blue']
=> ["red", "yellow", "green", "blue"]
>> yellow = colors.detect { |color| color == 'yellow' }
=> "yellow"

-- Josh

Greg Willits wrote:
> Stefano Crocco wrote:
>> On Thursday 14 August 2008, Greg Willits wrote:
>>> My actual code is a little different but I am assuming whatever
>>> technique solves the above would solve what I need as well.
>>>
>>> Seems like a simple syntax need, but I'm not finding a conclusive
>>> solution. Probably right in front of me, so sorry if it's major
>>> obvious...
>>>
>>> -- gw
>>
>> break color if color == 'yellow'
>
>
> Oi. yep, break is what I was looking for. I guess I've just never come
> across it anywhere (and googling anything ruby with "exit block" turns
> up 40 billion exit function discussions).
>
> Thx
>
> -- gw

--
Posted via http://www.ruby-....

TPReal

8/16/2008 9:44:00 AM

0

Greg Willits wrote:
> I found a suggestion that says to use return, but this generates a
> syntax error
>
> colors = ['red', 'yellow', 'green', 'blue']
>
> colors.each do |color|
> p color
> return color if color == 'yellow'
> end
>
> My actual code is a little different but I am assuming whatever
> technique solves the above would solve what I need as well.
>
> Seems like a simple syntax need, but I'm not finding a conclusive
> solution. Probably right in front of me, so sorry if it's major
> obvious...
>
> -- gw

Your question is not what you really want to ask about.

You don't want to exit the block. You want to exit the loop that is
inside the each method. And you break out from loops using break.

Just exiting the block would be useless, because you would still be
inside the loop, and the each would just go on to the next iteration. In
fact, you exit the block each time control gets to the end of the block
normally, but then the block is called again in the next iteration.
--
Posted via http://www.ruby-....