From: mkaeppler@... Date: 2021-08-05T14:10:29+00:00 Subject: [ruby-core:104789] [Ruby master Bug#12706] Hash#each yields inconsistent number of args Issue #12706 has been updated by mk (Matthias K�ppler). I just ran into this since I hadn't been aware of this change. What I find odd about this change is that it introduces a new inconsistency: the behavior of related Enumerable methods such as `map` is now different to that of `each` (in fact, isn't `map` implemented in terms of `each` at the VM level?) Using the bug author's example, I find the following behavior in Ruby 3 at least equally surprising: ```ruby irb(main):058:0> {a: 1}.each(&foo_lambda) (irb):44:in `foo': wrong number of arguments (given 1, expected 2) (ArgumentError) from (irb):58:in `each' from (irb):58:in `<main>' from /home/mk/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/irb-1.3.5/exe/irb:11:in `<top (required)>' from /home/mk/.rbenv/versions/3.0.2/bin/irb:23:in `load' from /home/mk/.rbenv/versions/3.0.2/bin/irb:23:in `<main>' irb(main):059:0> irb(main):060:0> {a: 1}.map(&foo_lambda) [:a, 1] => [[:a, 1]] ``` Why does `each` fail but `map` succeeds? ---------------------------------------- Bug #12706: Hash#each yields inconsistent number of args https://bugs.ruby-lang.org/issues/12706#change-93124 * Author: bughit (bug hit) * Status: Closed * Priority: Normal * Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN ---------------------------------------- ```ruby def foo(a, b) p [a, b] end def bar(a, b = 2) p [a, b] end foo_lambda = method(:foo).to_proc bar_lambda = method(:bar).to_proc {a: 1}.each(&foo_lambda) {a: 1}.each(&bar_lambda) ``` From #12705, yielding to method lambdas uses lambda/method arg semnatics the yield to foo produces `[:a, 1]` suggesting that each is yielding two values `yield key, value` but yield to bar produces `[[:a, 1], 2]` suggesting that each is yielding one value `yield [key, value]` it would be better if you always knew what to expect from it -- https://bugs.ruby-lang.org/ Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>