Project

General

Profile

Feature #8172

Updated by sawa (Tsuyoshi Sawada) over 4 years ago

=begin 
 There are a few desctructive `Array` (({Array})) methods that take an index as an argument and silently insert `nil` (({nil})) if the index is out of range: 

 ```ruby 
 

     a = []; a[1] = :foo; a # => [nil, :foo] 
 
     [].insert(1, :foo) # => [nil, :foo] 
 
     [].fill(:foo, 1, 1) # => [nil, :foo] 
 ``` 

 Among them, `Array#[]` (({Array#[]})) has a counterpart that returns an `IndexError` (({IndexError})) when the index is out of range: 

 ```ruby 
 

     [].fetch(1) # => IndexError 
 ``` 

 and this is useful to avoid bugs that would be difficult to find if `Array#[]` (({Array#[]})) were used. However for `Array#insert` (({Array#insert})) and `Array#fill`, (({Array#fill})), there are no such counterparts, and that fact that these methods silently insert `nil` (({nil})) is often the cause of a bug that is difficult to find. 

 I suggest there should be some versions of these methods that return `IndexError` (({IndexError})) when index is out of range: 

 ```ruby 
 

     [].insert!(1, :foo) # => IndexError 
 
     [].fill!(:foo, 1, 1) # => IndexError 
 ``` 

 I believe this would make debugging easier. 
 =end

Back