When the SketchUp Ruby API introduces new features or changes a common pattern to ensure backward compatibility is:
1 2 3 4 5 | if Sketchup.version.split('.')[0].to_i < 7 model.start_operation('FooBar') else model.start_operation('FooBar', true) end |
if Sketchup.version.split('.')[0].to_i < 7 model.start_operation('FooBar') else model.start_operation('FooBar', true) end
One might even need to check the minor and revision number in some cases. But doing so require you to split and parse strings as well as remembering which version supported what feature.
Instead, one can check for the feature itself. In the case of start_operation
‘s disable_ui
flag, you can check the method’s arity: Sketchup::Model.instance_method(:start_operation).arity
The rewritten code would look like this:
1 2 3 4 5 | if model.method(:start_operation).arity == 1 model.start_operation('FooBar') else model.start_operation('FooBar', true) end |
if model.method(:start_operation).arity == 1 model.start_operation('FooBar') else model.start_operation('FooBar', true) end
In SketchUp 6 and older this returns 1
while under SketchUp 7 and newer it returns -1
. (-1
is returned for methods that accept variable number of arguments.)
Another useful check is .respond_to?
– which you can use to check for features like so:
1 2 3 4 5 | if model.materials.respond_to?(:remove) model.materials.remove( material ) else self.remove_material_hack( material ) end |
if model.materials.respond_to?(:remove) model.materials.remove( material ) else self.remove_material_hack( material ) end
Feature testing like this is much more robust as you do not have to look up the exact version number for when SketchUp started supporting the feature. Simply test if the feature exist.
It’s the same type of design encouraged webdesign – where in the early days one relied on sniffing out the user agent string, but that has proven to be terrible unreliable. Instead, best practices now encourages people to test against features.
This method of designing is called Duck typing.
When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.