Project

General

Profile

Bug #17197

Some Hash methods still have arity 2 instead of 1

Added by marcandre (Marc-Andre Lafortune) 28 days ago. Updated 2 days ago.

Status:
Open
Priority:
Normal
Target version:
-
ruby -v:
ruby 3.0.0dev (2020-09-26T17:38:39Z master 950614b088)
[ruby-core:100192]

Description

Hash#each was changed recently to have arity of 1.
All other methods of Hash should behave the same.
Much has been fixed since #14015, but some remains:

# Some methods consistently have arity 2:
{a: 1}.select( &->(_kvp) {} ) # => ArgumentError (wrong number of arguments (given 2, expected 1))

All in all: %i[select keep_if delete_if reject to_h] have their arity still set at 2.

Updated by marcandre (Marc-Andre Lafortune) 28 days ago

Also, as noted by boblail (Bob Lail), Hash#map accepts both arity 1 and 2; shouldn't it accept only arity 1?

Updated by Eregon (Benoit Daloze) 28 days ago

marcandre (Marc-Andre Lafortune) wrote in #note-1:

Also, as noted by boblail (Bob Lail), Hash#map accepts both arity 1 and 2; shouldn't it accept only arity 1?

Agreed, especially since Hash#each changed to always yield a single argument.

I think it was just the same implementation as for Hash#each that would check the given Proc's arity.

Updated by Eregon (Benoit Daloze) 28 days ago

#map is defined on Enumerable.

So this seems an unintended interaction between
Hash#each using rb_block_pair_yield_optimizable():
https://github.com/ruby/ruby/blob/245ed57ddc93901e90388cf479968392299d1067/hash.c#L3158-L3159
and Enumerable#map using rb_block_min_max_arity and rb_yield_values2.
https://github.com/ruby/ruby/blob/245ed57ddc93901e90388cf479968392299d1067/enum.c#L547-L587

So as result that optimization seems to still not be transparent and changes semantics.

rb_block_pair_yield_optimizable() returns false for lambda though, and #map uses rb_lambda_call() so not sure what goes wrong there.

Updated by marcandre (Marc-Andre Lafortune) 2 days ago

nobu (Nobuyoshi Nakada) would it be possible to address this before preview-2?

Also available in: Atom PDF