You must not use the #attribute_dictionaries method this way because it returns nil if an entity’s dictionaries collection is empty. When this is so (an empty dictionaries collection,) and nil is returned, an attempt to call # on nil will raise a NoMethodError exception for NilClass as this class has no # method.
You don’t need to have the extra @val line as the result of the previous line would also return the result.
And, what is returned is not the value but a Boolean true or false whether it exists.
Using a literal string argument with the insertion of an interpolated reference is frivolous.
Ie, the reference value should be a String object. If not the interpolation will call #to_s upon it.
So it would be simpler to just do:
Well, you should try for readability and maintainability’s sake, to keep your code lines to 80 characters max.
In order for us to read what you posted, we must scroll horizontally, and that makes your code harder to understand.
Yes, reference assignment takes time, but if the method is not going to be within a loop running many times, then a few intermediate references won’t hurt much. We are talking milliseconds to assign a reference.
I probably have said this many times now, in this and the DC category.
For dynamic attributes, you always check the instance first (for components and nested dynamic groups) and if not a group, then you check the definition second.
DC_DICT ||= 'dynamic_attributes'
def has_dc_attribute?(inst, key, value)
# Check the instance
check = inst.get_attribute(DC_DICT, key)
# inst had attribute key
check == value
# inst did not have attribute key
definition = inst.definition
# Dynamic groups always have dynamic attributes in instance dictionary:
return false if definition.group?
# Check the component's definition:
check = definition.get_attribute(DC_DICT, key)
!check.nil? && check == value
Inside the block we have access to the boolean but not outside!
As dynamic attributes added manually on components are automatically attached to instances and definitions, we can do this:
DC ||= "dynamic_attributes"
def include_value_in_dynamic_attribut?( i, value )
@boolean = d.attribute_dictionaries[DC].values.include?(value.to_s)
@boolean = false
mod = Sketchup.active_model
sel = mod.selection
i = sel.grep(Sketchup::ComponentInstance) + sel.grep(Sketchup::Group)
include_value_in_dynamic_attribut?( i, "Heather" )
Excellent I take note!
I understand but what matters most to me is to publish the code as it is written in my rb file.
Especially since adapting it to the constraints of the forum will change my logic which is to reduce useless variables as much as possible.
Precisely if, the code will be called in loops or it will be executed several times.
As I said, storing information in intermediate variables increases the number of lines unnecessarily and slows down the code.
I want to optimize my way of coding as much as possible.