I was always under assumption that the block accepts the return value
of the iterator as the argument to the block, but look at this:
Array.new(500) do | i |
puts i
end
I expect i to be an array instance with 500 indexes all will nil
values. However, what it returns is indeed an array with 500 indexes
all with nil values BUT before it returns that array instance, the
block appears to execute 500 times passing an actual integer value
into the block incrementing by 1 each time. I dont understand why this
is happening.
all with nil values BUT before it returns that array instance, the
block appears to execute 500 times passing an actual integer value
into the block incrementing by 1 each time. I dont understand why this
is happening.
Because ‘puts()’ always return nil. It just generates STDOUT output.
Why is “id” not the array instance. It obviously is an integer.
Otherwise this would have failed with (id +1).
I don’t understand Why are you expecting an Array??
new(size=0, obj=nil)click to toggle source
new(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 sizecopies of obj (that is, size references to
the same obj). The third 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 elements index to the given
block and storing the return value.
You know what your completely right. The block only will take what the
yield method returns to it. So apparently when instantiating an array
with that first argument setting a size limit for array, the
constructor contains a yield method which yields each iteration of
that size limit. And then the return value is passed back to the
resutlt of the yield and that value is then idnexed into the array
until the size limit has been reached.