@thomthom said:
Sketchup.read_default
http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#read_default
True if successful, false if unsuccessful.
Surely it would return the value being accessed?
Verified!
Sketchup.read_default('Preferences','SnapAngle') returns 15.0
[My current setting; which happens in this case to be class Float.]
The API doc is vague about the default argument. The definition should read (as things work now):
"(optional) A value to return, if the variable is not found or empty."
[The way it is written, I read it as being a value to write into the registry.]
The lack of error checking in this method, can easily create false Resistry Keys.
Example: if you misspell the key (section on the Mac,)... ie:
[note misspelled 'Preferences']:
Sketchup.read_default('Prefer a
nces','SnapAngle')
... the method creates a new 'Preferances' key, with nothing under this new WORTHLESS key. (No Valuename, no value.) The method then just returns nil
. [On Win32, we must manually use the Registry Editor to get rid of this junk!]
If the key is correct, but the Valuename is wrong (or does not exist,) the method just returns nil
, [again,] or the default ReturnValue.
If the the Key is correct, AND the Valuname is correct, but the Value is empty, the method returns nil
, [once again,] or the default ReturnValue.
So we have 3 situations in which the method can return nil, and no way thru the SU API to tell what the situation is. [yes we can use one of the full ruby Win32 Registry modules, and I may begin doing this.]
A fourth situation, is if the value is an empty string, (ie '' or "" are stored in the registry value the method just returns the empty string (and NOT nil
.) The return class of the method is string in this case.
This can happen if you use Sketchup.write_default( key, valuename, ""), but its not necessary as each value in the registry has a type, and strings don't need delimiters.
We should use instead Sketchup.write_default( key, valuename, nil) to clear a string value in the registry.
I would propose a change to this method...
(1 as is.) IF everything exists, it works OK, and the value is returned.
(2 as is.) If (the Key exists, AND the Valuename exists,) AND the value is empty, then the method should return NIL without a default Return Value (3rd parameter,) OR the 3rd parameter if given.
(3) If the EITHER the Key OR the Valuename do NOT exist, then the method should raise an exception similar to a hash index error, viz:
Error in myprog.rb:22:in `Sketchup.read_default': key not found (IndexError) ...
Then (reference methods for Hash class):
CREATEseveral new boolean methods similar to Hash.has_key?, Hash.has_value? and Hash.empty? so the programmer can decide in their RESCUE block what action to perform;
I'd envision these boolean methods to be named similar to:
Sketchup.default_empty?
Sketchup.default_has_key? (perhaps with a Mac alias .default_has_section? for plist files)
Sketchup.default_has_valuename? (perhaps a Mac alias .default_has_variable? for plist files)
.
Thoughts?