From: "jeremyevans0 (Jeremy Evans)" Date: 2022-03-22T20:55:59+00:00 Subject: [ruby-core:108028] [Ruby master Bug#18635] Enumerable#inject without block/symbol will return values or raise LocalJumpError Issue #18635 has been updated by jeremyevans0 (Jeremy Evans). The call-seq for inject does not show usage without an argument or block, so I think it is reasonable to consider this as a bug, and it seems more likely to be a code bug than a doc bug. It's problematic that no error is raised when called on an enumerable with one or two elements, since that is likely to lead to cases that do not raise errors during testing, but raise errors in real world environments. LocalJumpError is raised when there is an attempt to yield to a block, but no block was passed. `Enumerable#inject` with zero or 1 arguments does not attempt to yield to a block, so it doesn't make sense to raise LocalJumpError. I think ArgumentError if passed no arguments and no block is a better fit. I submitted a pull request for that: https://github.com/ruby/ruby/pull/5690 ---------------------------------------- Bug #18635: Enumerable#inject without block/symbol will return values or raise LocalJumpError https://bugs.ruby-lang.org/issues/18635#change-96987 * Author: bjfish (Brandon Fish) * Status: Open * Priority: Normal * ruby -v: 3.0.3 * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- The following shows inconsistent behavior when no block/symbol are provided to inject. ``` ruby irb(main):002:0> {}.inject => nil irb(main):003:0> {one: 1}.inject => [:one, 1] irb(main):004:0> {one: 1, two: 2}.inject (irb):4:in `each': no block given (LocalJumpError) irb(main):005:0> [].inject => nil irb(main):006:0> [1].inject => 1 irb(main):007:0> [1, 2].inject (irb):7:in `each': no block given (LocalJumpError) ``` I would expect the results to be consistent by always raising a LocalJumpError or raising an ArgumentError when no block and symbol are given. -- https://bugs.ruby-lang.org/ Unsubscribe: