Reputation: 4774
We can find a definition of Ruby Array#fill
method:
The first three forms set the selected elements of self (which may be the entire array) to obj. A start of nil is equivalent to zero. A length of nil is equivalent to self.length. The last three forms fill the array with the value of the block. The block is passed the absolute index of each element to be filled. Negative values of start count from the end of the array.
What does it mean that the first three forms do something? What are these forms?
Upvotes: 0
Views: 42
Reputation: 168199
That is an unofficial and incomplete copy from the (nearly) official document, which has the description in more complete form:
fill(obj) → ary
fill(obj, start [, length]) → ary
fill(obj, range ) → ary
fill {|index| block } → ary
fill(start [, length] ) {|index| block } → ary
fill(range) {|index| block } → ary
The first three forms set the selected elements of self (which may be the entire array) to obj. A start of nil is equivalent to zero. A length of nil is equivalent to self.length. The last three forms fill the array with the value of the block. The block is passed the absolute index of each element to be filled. Negative values of start count from the end of the array.
So "the first three forms" refer to:
fill(obj) → ary
fill(obj, start [, length]) → ary
fill(obj, range ) → ary
By the way, v1_9_3_392 is so old. Why not use a newer version of Ruby?
Upvotes: 3
Reputation: 51171
These are three examples of usage put in the documentation:
a.fill("x") #=> ["x", "x", "x", "x"]
a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
Where the behavior of this method differs from the following two examples, where block is given:
a.fill {|i| i*i} #=> [0, 1, 4, 9]
a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
Upvotes: 1