diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-03-22 12:45:51 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2022-03-23 07:55:49 -0700 |
commit | 8f1c69f27ce6b3f5ed1c1cf8d2aa62aa9701d636 (patch) | |
tree | a824fa5c87113ddbb982b541819d266d5161e588 /enum.c | |
parent | d32fa986c3631fddcb256dbd39d10b358fb40ead (diff) |
Raise ArgumentError when calling Enumberable#inject without block or arguments
Previously, this would work as expected if the enumerable contained
0 or 1 element, and would raise LocalJumpError otherwise. That
inconsistent behavior is likely to lead to bugs.
Fixes [Bug #18635]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5690
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -1010,8 +1010,16 @@ enum_inject(int argc, VALUE *argv, VALUE obj) VALUE init, op; rb_block_call_func *iter = inject_i; ID id; + int num_args; - switch (rb_scan_args(argc, argv, "02", &init, &op)) { + if (rb_block_given_p()) { + num_args = rb_scan_args(argc, argv, "02", &init, &op); + } + else { + num_args = rb_scan_args(argc, argv, "11", &init, &op); + } + + switch (num_args) { case 0: init = Qundef; break; |