Please consider this code:
board = entities.each { |e| return if not findBoard( e ).nil? }
It should do exactly the same as:
for e in entities
board = findBoard( e )
if not board.nil?
break
end
end
but the first one does not work while the second one goes fine.
entitiesis an array of Entity objects and findBoard() returns a Board object or nil.
Entity and Board are not related classes.
I know that the second code works fine but since I am starting to learn Ruby, I am wondering if it is any more elegant way to do this so I ask, is it possible for an each method to return a different object type other than the objects in array (I guess it should)?
Really thanks.
You could use a lazy enumerator:
board = entities.lazy.map { |e| find_board(e) }.detect { |b| b }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With