@@ -809,6 +809,35 @@ module Indexable(T)
809
809
index(offset) { |e | yield e } || raise Enumerable ::NotFoundError .new
810
810
end
811
811
812
+ # Returns the first element in the indexable for which the passed block
813
+ # is truthy, starting from the given *offset*.
814
+ #
815
+ # Accepts an optional parameter *if_none*, to set what gets returned if
816
+ # no element is found (defaults to `nil`).
817
+ #
818
+ # ```
819
+ # [1, 2, 3, 4].find { |i| i > 2 } # => 3
820
+ # [1, 2, 3, 4].find(-1, 2) { |i| i < 2 } # => -1
821
+ # [1, 2, 3, 4].find(-1) { |i| i > 8 } # => -1
822
+ # ```
823
+ def find (offset : Int = 0 , if_none = nil , & : T - > )
824
+ offset += size if offset < 0
825
+ return nil if offset < 0
826
+ return (index(offset) { |i | yield i }).try { |i | unsafe_fetch(i) } || if_none
827
+ end
828
+
829
+ # Returns the first element in the indexable for which the passed block is truthy.
830
+ # Raises `Enumerable::NotFoundError` if there is no element for which the block is truthy.
831
+ #
832
+ # ```
833
+ # [1, 2, 3, 4].find! { |i| i > 2 } # => 3
834
+ # [1, 2, 3, 4].find! { |i| i > 8 } # => raises Enumerable::NotFoundError
835
+ def find !(offset : Int = 0 , & : T - > )
836
+ offset += size if offset < 0
837
+ return nil if offset < 0
838
+ return (index(offset) { |i | yield i }).try { |i | unsafe_fetch(i) } || raise Enumerable ::NotFoundError .new
839
+ end
840
+
812
841
# Returns the last element of `self` if it's not empty, or raises `IndexError`.
813
842
#
814
843
# ```
0 commit comments