Exiting of a block

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:bashj0$33hl9$3@ID-52924.news.dfncis.de...

> # now find the current program for this
> # channel.
> programsForChannel.find { |program|
> result = false
> if program.time <= showTime
> if program.endTime
> # there is a endtime.
> # are we now before this end?
> if program.endTime > showTime
> result = true
> end
> else
> # program has no registered
> # end.
> result = true
> end
> end
> result
> }
>
> any help appreciated!

In your case you can use "return" even with older versions of ruby if you
use a method:

def find
  [1, 2, 3].find { |e|
    return true if e == 2
  }

  false
end

Are you aware of the standard methods Enumerable#include?,
Enumerable#find, Enumerable#detect, Enumerable#select, Enumerable#find_all
and Enumerable#grep? These should provide the functionality you need.

the code you give returns returns true or false, i want the item that is
found; try it.
for the code you gave, i would expect a result of 2. but you get true :O(

i actually want to use Enumerable#find, as I do, but with a complex
condition. the problem is that in my condition i have several places to say
"yes, this is the item i want" or "no, it's not the item i want". in a
function i would use return, but in that case, that pops me out of the whole
function, not only of the block.

actually i want exactly what matz suggested, that's only available in 1.8:
break <return value for the block>.
i'll clean up this code when upgrading to 1.8 :O)

Regards

    robert

thanks for the suggestion,

emmanuel

“Emmanuel Touzery” emmanuel.touzery@wanadoo.fr schrieb im Newsbeitrag
news:002d01c3236d$64226910$1600a8c0@emmanuels…

“Robert Klemme” bob.news@gmx.net wrote in message
news:bashj0$33hl9$3@ID-52924.news.dfncis.de

now find the current program for this

channel.

programsForChannel.find { |program|
result = false
if program.time <= showTime
if program.endTime

there is a endtime.

are we now before this end?

if program.endTime > showTime
result = true
end
else

program has no registered

end.

result = true
end
end
result
}

any help appreciated!

In your case you can use “return” even with older versions of ruby if
you
use a method:

def find
[1, 2, 3].find { |e|
return true if e == 2
}

false
end

Are you aware of the standard methods Enumerable#include?,
Enumerable#find, Enumerable#detect, Enumerable#select,
Enumerable#find_all
and Enumerable#grep? These should provide the functionality you need.

the code you give returns returns true or false, i want the item that is
found;

Well, your example (the simple one) returned ‘true’ or ‘false’. This is
an easy change:

def find
[1, 2, 3].find { |e|
return e if e == 2
}
nil
end

But that’s exactly what Enumerable#detect and Enumerable#find do.

i actually want to use Enumerable#find, as I do, but with a complex
condition. the problem is that in my condition i have several places to
say
“yes, this is the item i want” or “no, it’s not the item i want”. in a
function i would use return, but in that case, that pops me out of the
whole
function, not only of the block.

If that condition is so complex, i’d either
a) include it in the class of the instances in the collection (I guess
it’s called “Program” or similar) or
b) define a function for it

That way you get better documentation, reusability and get rid of the
return problem. However, variant a) seems most natural (i.e. OO) to me.

robert

Moin!

Emmanuel Touzery wrote:

i actually want to use Enumerable#find, as I do, but with a complex
condition. the problem is that in my condition i have several places to say
“yes, this is the item i want” or “no, it’s not the item i want”. in a
function i would use return, but in that case, that pops me out of the whole
function, not only of the block.

Please also note that ‘if’ has a return value in Ruby.
See http://www.rubycentral.com/book/tut_expressions.html#UH for reference.

So your code could be written as:

now find the current program for this

channel.

programsForChannel.find { |program|
if program.time <= showTime
if program.endTime
# there is a endtime.
# true if we now are before this end
true if program.endTime > showTime
else
# program has no registered
# end.
true
end
end
}

thanks for the suggestion,
emmanuel

Regards,
flgr