General FYI on counter / iterator variables

General FYI on counter / iterator variables:

Ya’ll can use an Enumerable#each_with_index loop and not have to define or increment a counter var.

[url]Module: Enumerable (Ruby 2.0.0)

The Enumerable mixin module is mixed into Array and most (if not all) of the API collection classes.

3 Likes

Dan,

Thanks for the reminder about the each_with_index method.

The code samples you seem to be referring to were looping thru two collections/arrays.

I set up three loops –

  • .each using a counter
  • .zip.each
  • .each_with_index

In each block I set two variables to the respective member of each arrays. Running the loops over arrays with 6,000 elements (6,000 faces, 6,000 materials), there was no difference in time, and all were essentially zero mS.

For those not following the other thread, without any real SketchUp operations, these loops take no time. When the loops then set the front and back material of a face, they take (under the best conditions) about 5 seconds.

Hence, in almost all real-world SketchUp API code, how one performs looping has very little affect on the total time. Write your code so its clear and understandable, especially if you may not need to change it (or review it) for a long time.

So time isn’t an issue. You used the terms ‘unneeded’ and ‘always takes time’. Reshuffling 12k object pointers and defining 6k arrays is something that really doesn’t bother Ruby, or most other programming languages.

In the code Rojj posted, those arrays go out of scope once the loop is done, and are probably garbage collected in the time it takes one to move their mouse pointer 50 pixels.

As an aside (and as you know), what may affect execution time is whether one should loop over a live collection, or, create a static copy and then do the looping.

Greg

It was general, and generic to any code or any coder. Not specifically aimed at anyone person’s code examples. (There were several posters using manual coded index variables in the linked thread.)

This thread just serves as a reminder to those new to the SketchUp API, that the Array class, and most of the API’s collection classes, are augmented with nifty iterator methods from the Enumerable library module.

The SketchUp API documentation does not indicate the inheritance chain like some other documentation sites / generators, so those new to SketchUp Ruby programming usually don’t know this until someone tells them.

That is why this is broken out into it’s own thread.

(I’ve edited and removed the comment about the .zip method because it refers to the other topic thread, and moved it back into that other thread.).

Dan,

I always take code samples as just that, no one’s got the time to optimize them, they’re just trying to show some particular issue. Hence, they’re probably not the author’s idea of ‘best coding practices’…

Re docs, see –

SketchUp 2014 Collections Info - GitHub.io

If you’ve got suggestions for any other columns, please comment.

[EDIT] 10-Dec - all up on GitHub.io, added links to SketchUp.com for SketchUp objects in lists.

Thanks,

Greg