Project

General

Profile

Feature #7394

Enumerable#find ifnone parameter could be non-callable

Added by zzak (Zachary Scott) about 7 years ago. Updated 9 months ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-core:49557]

Description

from github:
https://github.com/ruby/ruby/pull/186

In trunk the Enumerable#find method ifnone parameter has to be callable or nil. I found that sometimes I want to return a simple value without wrapping it in a proc. This pull request adds support for non-callable defaults when no items match.

a = [1, 2, 3]

The current behavior

a.find(proc { :foo }) { |x| x > 3 } #=> :foo

With patch

a.find(0) { |x| x > 3 } #=> 0

Files

enumerable_find_noncallable.patch (3.45 KB) enumerable_find_noncallable.patch zzak (Zachary Scott), 11/19/2012 12:36 PM

History

Updated by mame (Yusuke Endoh) almost 7 years ago

  • Status changed from Open to Assigned
  • Target version changed from 2.0.0 to 2.6

Zachary Scott, please don't add 2.0.0 feature ticket unless I approve. The 2.0.0 feature deadline was passed.

--
Yusuke Endoh mame@tsg.ne.jp

Updated by mame (Yusuke Endoh) almost 7 years ago

Oh, I didn't realized that this ticket was from github pull reqeust.
Thank you for your importing work!
But, the fact remains that this proposal was not accepted by the 2.0.0 deadline. Sorry.

It is unfortunate that people misunderstands that github pullreq is the right way to request a feature to Ruby.
Is it impossible to stop (or automatically reject) pullreq?

--
Yusuke Endoh mame@tsg.ne.jp

Updated by zzak (Zachary Scott) almost 7 years ago

This was during my import of patches from github to redmine.

You can turn off pull requests on github, maybe start a new thread to discuss that.

#4

Updated by naruse (Yui NARUSE) almost 2 years ago

  • Target version deleted (2.6)

Updated by ioquatix (Samuel Williams) 9 months ago

Can we merge this?

Updated by ioquatix (Samuel Williams) 9 months ago

Just because it might cause some surprise, perhaps we can use keyword argument for this.

e.g.

a = [1, 2, 3]
a.find(else: 0) { |x| x > 3 } #=> 0

I kind of prefer original suggestion, but I can imagine if passing an object that responds to #call it might have unexpected behaviour.

Updated by nobu (Nobuyoshi Nakada) 9 months ago

  • Description updated (diff)

Currently, it is not able to distinguish from the case a hash is given as an ordinal argument.
So there still is a possibility to break a compatibility.

Also available in: Atom PDF