fill

ary.fill(obj) â ary
ary.fill(obj, start [, length]) â ary
ary.fill(obj, range ) â ary
ary.fill { |index| block } â ary
ary.fill(start [, length] ) { |index| block } â ary
ary.fill(range) { |index| block } â ary
Instance Public methods

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 the length of the array.

The last three forms fill the array with the value of the given block, which is passed the absolute index of each element to be filled.

Negative values of start count from the end of the array, where -1 is the last element.

a = [ "a", "b", "c", "d" ]
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"]
a.fill { |i| i*i }       #=> [0, 1, 4, 9]
a.fill(-2) { |i| i*i*i } #=> [0, 1, 8, 27]
doc_ruby_on_rails
2015-03-30 17:00:18
Comments
Leave a Comment

Please login to continue.