Project

General

Profile

Actions

Feature #9347

open

Accept non callable argument to detect

Added by marcandre (Marc-Andre Lafortune) almost 11 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:59482]

Description

Currently, the only argument that Enumerable#detect accepts is a callable object.

Shouldn't we accept non callable objects too?

[42].detect(:not_found){} # => NoMethodError: undefined method `call' for :not_found:Symbol
 # would return :not_found instead.

I'd suggest that if the given argument does not respond_to? :call, then it would be returned as is instead of raising an error as currently.
Wouldn't this be more flexible and possibly more performant?

Inspired by http://stackoverflow.com/questions/20883414/why-does-enumerabledetect-need-a-proc-lambda

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0