(1) Ruby is multi-paradigm. There are always many ways to do the same things.
This study just may boil down to coding finesse or cleverness.
(Not really the words Iām looking for here.)
(2) I have not said, and do not say, that my class study is meant to solve those 3 points.
Did I imply these ?
Only that it was a study that provided strict boolean testing.
Ie, a paradigm that:
- does not consider
nil
to be boolean false
- that does not consider non-boolean objects to be boolean
true
- one that allows
0
and 1
to test boolean-wise.
- one that allows this comparability to be inherited into custom classes
- but most importantly, does not change the way the Ruby interpreter works
- so, a specially compiled interpreter is not needed
- and code can still use normal Ruby boolean expressions (where
nil
is treated as false
and any other object evaluates as true
, etc.,) ie, most important that it doesnāt break normal ālooseā boolean expressions
So to satisfy those ālawsā, we must have special āstrictā test methods that the custom boolean classesā methods will use.
(3) I also will retort that I do not believe that those 3 points are some āundeniable rulesā that any coding paradigm must pass.
(But Iāll say they look like the minimalistās 3 golden rules.)
(4) Iāll make a note to create SketchUp API specific examples.
This whole study actually did come from me reading several forum posts over the years, in which people were at a loss to even begin to know how to code a simple toggle command in SketchUp Ruby. Such as:
- toggling between two rendering states
- switching an extension option on & off
- acting upon a yes / no response from the user
So, this study was actually aimed at basic support of higher level SketchUp specific functionality, in the realm of options and answers.
I never actually got to showing the instantiation of a option like class that could be toggled.
In SketchUp Ruby the class would likely write to the registry, a hash, or an attribute dictionary, for a specific extension.
One of the weird things about Ruby is that it doesnāt have a Boolean
superclass as an ancestor of TrueClass
and FalseClass
, in order to group them together.
So for example, without my extension, you cannot do:
option.is_a?(Boolean)
ā¦ for a reference (option
) that may, at various times, be pointing at the singleton instance of two separate classes.
But you CAN if you simply do (at the console):
module Boolean; end
TrueClass.class_eval { include Boolean }
FalseClass.class_eval { include Boolean }
ā¦ then:
option = true
option.is_a?(Boolean)
# returns >> true
option = false
option.is_a?(Boolean)
# returns >> true
The same will hold for any custom class that includes the Boolean
module.
Currently, in order to specifically test for one of these two classes, you have to do this:
option.is_a?(TrueClass) || option.is_a?(FalseClass)
ā¦ which I always found to be somewhat clunky.
Now, simply using references that directly point variously at true
or false
, does not protect from an error that can occur if the reference gets pointed at some other object(s).
This is where the custom wrapper Boolean
subclass comes in. It will have the toggling mechanism built-in, and the coder just calls #toggle()
upon an instance of the class. Internally is has a reference to a boolean that can only be one of the allowable values.
In SketchUp API use, a ExtensionOptions subclass of Boolean would have the value saving built-in. And there might even be a collection wrapper around that. (I have some code Iāve played with in that vein, but itās way rudimentary.)