mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	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]
This commit is contained in:
		
							parent
							
								
									d32fa986c3
								
							
						
					
					
						commit
						8f1c69f27c
					
				
				
				Notes:
				
					git
				
				2022-03-23 23:56:10 +09:00 
				
			
			
			
		
		
					 2 changed files with 11 additions and 1 deletions
				
			
		
							
								
								
									
										10
									
								
								enum.c
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								enum.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -234,6 +234,8 @@ class TestEnumerable < Test::Unit::TestCase
 | 
			
		|||
    assert_equal(24, @obj.inject(2) {|z, x| z * x })
 | 
			
		||||
    assert_equal(24, assert_warning(/given block not used/) {@obj.inject(2, :*) {|z, x| z * x }})
 | 
			
		||||
    assert_equal(nil, @empty.inject() {9})
 | 
			
		||||
 | 
			
		||||
    assert_raise(ArgumentError) {@obj.inject}
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  FIXNUM_MIN = RbConfig::LIMITS['FIXNUM_MIN']
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue