This is probably a ridiculously simple question but I am trying to determine the best way to create a conditional in my code that checks to see if the version of SketchUp is greater than a specific version. In my case I am trying to see if the user is running SU 2016 or greater.
Sketchup.version will give you a string for the version number, is it recommend to just change that to a float?
if Sketchup.version.to_f < 16.0
Seems like this works just fine but thought I should probably check to see if there is a more generally accepted way to do this.
Yes! Agreeing with BillyBob on this. I am myself using this for the last 2 years and it works like charm (Even more on Finance Police). No issues whatsoever!
As Dezmo says integers are good for checking major versions.
For maintenance releases I’m not sure how floats would work. Version numbers are not strictly divided into a integer part and a fractional part, but can have any number of parts. A quite common approach, used e.g. by SketchUp extensions, is to have 3 numbers, often thaught of as major, minor and patch. A general way to compare version numbers would be to split them at the dots into an array, parse each element as an integer and compare corresponding integers. This functionality is built into Ruby gems, so you don’t need to reinvent the wheel.
But in like 95% of cases doing an integer comparison is enough.
The “correct” way would be to use a version parser (as you have noticed version nomenclature does not correspond 1:1 to float or integer syntax). Most scripting languages provide one, like Python, and it offers version comparison and correctly handles all the standardized version syntax of that language (e.g. Python: ==1.0.2-dev, >=1.0, ~=1.0; Ruby: ==1.0.2beta, ~>1.0).
For Ruby we would need gems, e.g. the default require "rubygems" and then Gem::Version.new("1.0.2").
Comparing versions is maybe more convenient if you have a lot of feature differences to check for. But it adds a level of indirection: You as a developer need to look up the release notes and choose the version (you can do a mistake). Then a reader of the source code wonders why a certain version was chosen and if this requirement is still necessary. Other developers often don’t dare to touch/refactor code that contains undocumented unknowns (a risk to break something) which makes maintenance really hard. So you would have to document in the source code the reason for the choice of versions.
But if you are only looking for one feature, checking the availability of that feature is easier and self-documenting. If an existing feature was changed you may have to check availability of a new method or the arity of its parameters.
I’ve also checked the ruby version. In the old days floating point math was very slow especially when you didn’t have a math coprocessor. Today you would be hard pressed to detect the difference with one calculation.
By then we will have reached the technological singularity and SketchUp and mankind as we know it will no longer exist. So no need to worry about that.
We don’t have to compare license serial numbers (do we actually have access to them?), but SketchUp could continue with \u and use Unicode — and emojis! 1.2.👨🔧
I think the safest and reliable version comparison is dezmo’s .to_i for only major version, dezmo’s and MSP_Greg’s split for whole versions, and where possible avoid it all together and test features.