Project

General

Profile

Bug #5344 ยป 0001-Clarified-Array.new-documentation.patch

henry.maddocks (Henry Maddocks), 09/21/2011 09:14 AM

View differences:

array.c
* Array.new(size) {|index| block }
*
* Returns a new array. In the first form, the new array is
* empty. In the second it is created with _size_ copies of _obj_
* (that is, _size_ references to the same
* _obj_). The third form creates a copy of the array
* empty, or it is created with _size_ copies of _obj_ (that is,
* _size_ references to the same _obj_).
* The second form creates a copy of the array
* passed as a parameter (the array is generated by calling
* to_ary on the parameter). In the last form, an array
* of the given size is created. Each element in this array is
* calculated by passing the element's index to the given block and
* storing the return value.
*
* Array.new
* Array.new(2)
* Array.new(5, "A")
* Array.new #=> []
* Array.new(2) #=> [nil, nil]
* Array.new(5, "A") #=> ["A", "A", "A", "A", "A"]
*
* # only one copy of the object is created
* a = Array.new(2, Hash.new)
* a[0]['cat'] = 'feline'
* a
* a[1]['cat'] = 'Felix'
* a
* a = Array.new(2, Hash.new) #=> [{}, {}]
* a[0]['cat'] = 'feline' #=> "feline"
* a #=> [{"cat"=>"feline"}, {"cat"=>"feline"}]
* a[1]['cat'] = 'Felix' #=> "Felix"
* a #=> [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
*
* # here multiple copies are created
* a = Array.new(2) { Hash.new }
* a[0]['cat'] = 'feline'
* a
* a = Array.new(2) { Hash.new } #=> [{}, {}]
* a[0]['cat'] = 'feline' #=> "feline"
* a #=> [{"cat"=>"feline"}, {}]
*
* squares = Array.new(5) {|i| i*i}
* squares
*
* copy = Array.new(squares)
* squares = Array.new(5) {|i| i*i} #=> [0, 1, 4, 9, 16]
*
* copy = Array.new(squares) #=> [0, 1, 4, 9, 16]
*/
static VALUE
    (1-1/1)