mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Fix pp when passed a empty ruby2_keywords-flagged hash as array element (#2966)
This causes problems because the hash is passed to a block not accepting keywords. Because the hash is empty and keyword flagged, it is removed before calling the block. This doesn't cause an ArgumentError because it is a block and not a lambda. Just like any other block not passed required arguments, arguments not passed are set to nil. Issues like this are a strong reason not to have ruby2_keywords by default. Fixes [Bug #16519] This backports28d31ead34and0ea759eac9, but needed to be modified for 2.7 as 2.7 will perform empty keyword to positional hash conversion for required arguments, which will happen if "v" in the seplist method is empty when yielded. Co-authored-by: NARUSE, Yui <nurse@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									d04856bdc5
								
							
						
					
					
						commit
						bb93659fef
					
				
					 2 changed files with 15 additions and 1 deletions
				
			
		
							
								
								
									
										11
									
								
								lib/pp.rb
									
										
									
									
									
								
							
							
						
						
									
										11
									
								
								lib/pp.rb
									
										
									
									
									
								
							|  | @ -223,7 +223,16 @@ class PP < PrettyPrint | |||
|         else | ||||
|           sep.call | ||||
|         end | ||||
|         yield(*v) | ||||
|         case v.last | ||||
|         when Hash | ||||
|           if Hash.ruby2_keywords_hash?(v.last) | ||||
|             yield(*v, **{}) | ||||
|           else | ||||
|             yield(*v) | ||||
|           end | ||||
|         else | ||||
|           yield(*v) | ||||
|         end | ||||
|       } | ||||
|     end | ||||
| 
 | ||||
|  |  | |||
|  | @ -176,6 +176,11 @@ class PPSingleLineTest < Test::Unit::TestCase | |||
|     assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, ''.dup)) # [ruby-core:02699] | ||||
|     assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''.dup)) | ||||
|   end | ||||
| 
 | ||||
|   def test_hash_in_array | ||||
|     assert_equal("[{}]", PP.singleline_pp([->(*a){a.last}.ruby2_keywords.call(**{})], ''.dup)) | ||||
|     assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup)) | ||||
|   end | ||||
| end | ||||
| 
 | ||||
| class PPDelegateTest < Test::Unit::TestCase | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jeremy Evans
						Jeremy Evans