Storing empty array in Attribute Dictionaries


#1

So I’m setting my window array initially to an empty array @Windowlist = [] and then storing it as an attribute. When I save the file and then restart SketchUp and re-open the file I notice that the empty array is now “nil” and for some reason I can’t seem to set the variable back to an empty array, very strange behaviour.


#2

Yes, this is a bug. The strange thing (if I remember correctly) is that the empty Array can still be fetched within the same SU session as it was set. I struggled with this for eitehr my railroad system or townhouse system some years back. I think I worked around it by adding || [] after the method reading the attribute, or by passing empty array as default value to attribute_read.


#3

Of what class are the object item references in the @Windowlist array ?


#4

I’m not even sure how to answer that question.

When the array is filled it looks something like this: [“window1”, “window2”, “window3”]


#5

So it is just an array of window caption strings ?


#6

Yes, just simple text entries so that I can keep track of the number of windows in a wall panel and also have a name to associate with a nested group.


#7

I hate to hijack my own thread but a related problem I am trying to figure out in Ruby is how to easily sort an array of values from lowest to highest (numeric value) and also create a list of the keys in sorted order that reference these numeric values in a separate hash. I’m probably not explaining this one right.


Sorting arrays
#8

Ah okay. I was thinking display window like dialog window, not “window” window.

A workaround might be to save your attribute as JSON text.

ary_str = [].to_json
ary_str.inspect
"[]"
# write ary_str to dictionary
# read it back into an array ...
a = JSON.parse(ary_str)

#9

If my array is empty I just don’t attempt to store it as an attribute that seems to have avoided the issue all together. What a pain in the butt though, it had me pulling my hair out trying to figure out why the whole environment was getting unstable and why the problem only surfaced if I saved and the restarted SketchUp.


#10

Some attribute oddities go away if you wrap the value into an object literal/hash. I don’t know if it might help here.


#11

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.