I’m currently working on the window and door editing tool of the new Wall Plugin and I’m trying to collect the list of entities (groups) within my wall group that are doors and windows. Each group will have a regular naming convention (ie. door1, door2, door3 etc… or window1, window2, window3 etc…)
I am ashamed to say that my Ruby knowledge or experience with the grep method or function is not very strong. I am thinking this would be the best method for grabbing these groups from the list of entities within the wall group.
Any guidance or direction would be helpful. I think I can probably figure out the correct code but I just don’t want to go down a specific route when there might be a significantly better or more efficient way of doing this.
We’ve been here and done this question / exercise already.
Firstly, in Ruby whenever dealing with collections (Arrays, Hashes, etc.) be sure to consult the Enumerable module for all it’s nifty methods which are mixed into all the collection classes (as well as many of the SketchUp API’s collection classes.)
Thirdly the examples show exact matches using the == method, you’ll likely want to use regular expression to find matches to substrings or start strings. (Ruby 2.0 added some methods to the String class for #start_with?)
Reg expressions … (lowercase i after expression is case insensitive)
HAS_DOOR = /door\d+/i # contains "door" followed by 1 or more digit characters
… or …
BEGINS_DOOR = /\Adoor\d+/i # Starts with "door" followed by 1 or more digit characters
You do not want to use literal inside a loop as it will create an new Regexp object each time through the loop.) The references do not absolutely have to be a constant if they are only needed on occasion. They could be local references inside a method that get cleaned up when the method ends.
I wouldn’t rely on names to store information as the user can easily change them. Attributes are safer for this, unless it is by design that a user can convert a window into a door by renaming it.