@hank said:
I think this
=needs to be changed to==...Right? It changed set all group names rather than testing for equivalence.
Correct. My bad. I've fixed it in the above example.
@hank said:
Anyway,
grepwith a more complex block seems like such a more efficient way of getting objects than getting an array ...
grep always first creates an array result from the pattern argument.
If you read the description of the method in the docs, it is quite clear there will be a two stage operation if a block is used with grep. The drawback is that the block will process ALL the members returned by the pattern, and push the result of the block into the output array. This will not be a problem if you are not going to use the output array any further. (Ie, the output array might be a series of valid group objects [that passed the name property check,] and false references for any group that did not.)
The most important part of grep'ing with a class identifier, is that it is extremely fast at filtering out any other item of other classes (that are not subclasses.)
More specifically, text comparison in Ruby is slow, whilst class identity comparison is fast.
@hank said:
... then looping it to test for certain properties, then looping THAT array to test for others, and so on... which is what I felt like I was doing before.
It is not a big deal to do that once as I did in the above example, using grep to get all groups, then filtering out only those with a certain name.
But, yes you can do compound conditional tests within iterator blocks:
name_prefix = "MY_TARGET_GROUPS"
starts_with = /\A#{name_prefix}/
grps.each {|g|
if g.name =~ starts_with && g.material.name = "Green"
# Do something with matching group instance
else
# Do something else
end
}
