Checking SketchUp version in API

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.

That’s what I’ve been using for years, it works fine.

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!

1 Like

If you want to know only the major version

Sketchup.version.to_i < 16

I “feel” this is more efficient. (But I think both are fine…)

But you can also check the whole versions with : . #version_number (*args) ⇒ Integer


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.

major, minor, build = Sketchup.version.split(".").map(&:to_i)

Integer math and comparison is usually faster, though in this case one expression it would not matter much.

However, you should be aware that these core Ruby conversion methods, do not raise exceptions. Instead they just return 0 or 0.0 on errors. Ex …

#=> 0
#=> 0.0

So, as Andreas said yesterday in another topic I think, it is always better to “duck-type” ie, test functionality rather than class or version.

What was new with SketchUp 2016 ? The Sketchup::Axes class was first defined.
So any older versions will not have it defined …

if defined?(Sketchup::Axes)
  # this is v 16 or higher
  # this is v 15 or older


1 Like

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"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.

html_version = RUBY_VERSION[0…2].to_f >= 2.2

Will that be true for Ruby 3?

Edit: The forum software replaced the 0..2 with a typographic ellipsis ..., which would parse only 2 characters and fail for Ruby 2. but not for 3..

Yes it will.

It will fail after Ruby version 999

Ruby version 1.6.7 released in 2002
Ruby version 2.6.1 released in 2019

At the present rate it would take over 16,000 years to reach the failure point.


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.

With versions, the issue is often a two digit minor/teeny version. Or:
2.10 > 2.9, hence the .split(".").map &:to_i

1 Like

The serial number of version 2019 is ‘U’, so we would be stuck in 2024…

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!

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.

1 Like