Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find last occurence matching condition in array

Tags:

arrays

ruby

I can't find out which method to use for finding the last element of an array matching a condition.

This should work but that doesn't seems to be the best way:

array.reverse.find {condition}

array[array.rindex {condition}]

Would expect something like array.rfind {condition}...

like image 788
Léon Boutrand Avatar asked Oct 14 '25 18:10

Léon Boutrand


1 Answers

array.rindex(condition) will give the last matching element index no. This will give you last matching element

array[array.rindex(condition)]
like image 150
Muhammad Adeel Avatar answered Oct 17 '25 10:10

Muhammad Adeel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!