I have an array, and I want the result of the first block that returns a truthy value (aka, not nil). The catch is that in my actual use case, the test has a side effect (I'm actually iterating over a set of queues, and pop'ing off the top), so I need to not evaluate the block beyond that first success.
a,b,c = [1,2,3]
[a,b,c].first_but_value{ |i| (i + 1) == 2 } == 2
a == 2
b == 2
c == 3
Any ideas?
#findmethod normally does? I already wrote an answer, but I don't believe that you actually mean what you ask for.+=operator in your block does not make sense, sinceigets discarded after every block evaluation.==are crude asserts. And I do always forget that Ruby's+=doesn't always work the way I expect (hold over from the C++ days), so you're correct that I'm using incorrectly for what I'm trying to achieve. In the actual use case, it's a.pop, but I felt like that would have required additional background. The "result" would definitely be the return value of the block. I don't know any other way to interpret the "result". In my use case, the block will eval tonilrather thanfalse, and not everyone that reads these questions will knownilis falsey