Saturday, February 25, 2012

Re: [Rails] question about passing an array iterator instance to a block

On 25.02.2012, at 23:41, John Merlino wrote:

> 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.

Because 'puts()' always return nil. It just generates STDOUT output.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate