Matrix Standard Library

The Matrix class in Ruby is not part of the Core but it is part of the Std-lib (Standard Library). Has anyone tried using a standard library class like this in their plugins? I’ve never tried this before so I’m kind of flying blind.

Poking around in the the SketchUp Program Files folder it appears that the RubyStdLib folder contains all of the standard library classes. Is there any particular way that these files should be loaded or included?

If I was to load the matrix.rb file I would need to specify the path which would be OS dependent and SU version dependent, for example on Windows:

C:\Program Files\SketchUp\SketchUp 2017\Tools\RubyStdLib

what would this be on a Mac?

Yes use the Ruby core global require.

SketchUp distro’d extensions use the standard JSON library by requiring it (which means it’s already loaded by the time your extension begins loading.)

No you would not. It is already in the $LOAD_PATH array.

Just do …

require 'matrix'
1 Like

Thank-you Dan, your help is sometimes the only thing that keeps me going.

I’m a little confused with require vs. include. For the Math class I use include but for the Matrix class I use require.

Also I’m no Ruby expert as many of you know but I am slowing learning the ins and outs of the language.

I’m looking at the documentation for the Matrix class here:

And then I found this website with a number of methods (that work within this class) but don’t seem to be documented in the official ruby docs.

Should I use these methods? They do seem to work. For example the lup() function.

Also there exists two classes within the Matrix class:

Matrix::LUPDecomposition
Matrix::EigenvalueDecomposition

How would one instantiate them?

require exists in Kernel, and it loads a source code file by loadiing the first file with the given name in $LOAD_PATH. With ‘normal’ Ruby, RubyGems monkey-patches the method to allow RubyGems to load installed gems.

include is a method that is one of the ‘inheritance’ type methods (include, extend, prepend) that allow one to mixin methods from a module. Hence, a module can’t be used with include if it hasn’t been required.

Also, require’s parameter is a string, which is a filename, include takes a module as a parameter.

Re the matrix std-lib/default gem, there have been changes in it, so for use in SU, you might want to vendor it. Below are links to docs for matrix:

Ruby 2.2.10
Ruby 2.5.8
Ruby master

Note that generally std-lib/default gems, and bundled gems do not change versions within a Ruby major/minor release, except for security/bug fixes.

1 Like

What exactly do you mean by vendor it?

Sorry. By ‘vendor’, I meant include the matrix files in your plugin, so no matter what version of Ruby is used, you have the same functionality.

The term may vary, but both RubyGems & Bundler ‘vendor’ files because they need some std-lib functionality to run, but they also need the app they’re running to be able to load gems of those std-lib’s.

Note that some of the newer matrix files use autoload. I think autoload can work with an absolute path, so you might be able to also use it (with modified path)

1 Like

Math is a core module (not a class, as you cannot include classes into other stuff.)
As a core module is doesn’t need to be loaded for use. (It’s part of the core.)
The methods in Math are double duty. They have module function copies that can be called from outside (library methods,) and instance method copies that are used when mixing into other classes or modules.

You should refer to documentation for the Ruby versions that SketchUp uses (that you target.)
Methods are added to core classes in later versions. There are also interpreter changes from time to time.

You see the version in all of the doc URLs. You can change it manually in the address bar.

SU 2019, SU2020
https://ruby-doc.org/core-2.5.5/

SU 2017, SU2018
https://ruby-doc.org/core-2.2.4/

SU2014 … SU2016
https://ruby-doc.org/core-2.0.0/

SU2013 and older used Ruby 1.8.6
https://ruby-doc.org/core-1.8.6/index.html

I suggest you bookmark these in your browser.
The link to the standard lib for each version is at the top of the entry pages given.

1 Like

Looking at their docs, each has a public class constructor named new that only takes a Matrix object as it’s lone argument.

1 Like

I’m assuming that the basic methods called in the Matrix class regardless of Ruby version are probably the same for the most part. I’m only working with SU 2017 - 2020 now, I’ve dropped support for older versions of SketchUp because of the API changes.

Yes, bookmarked.

1 Like

It looks like the Matrix class autoloads the other two sub classes (first lines within the Matrix class):

class Matrix
  include Enumerable
  include ExceptionForMatrix
  autoload :EigenvalueDecomposition, "matrix/eigenvalue_decomposition"
  autoload :LUPDecomposition, "matrix/lup_decomposition"

I did a text compare with the matrix.rb file in SU 2020 and SU 2017 and there is only one major difference (one new method added to SU 2020), other than that they are mostly identical (other than some changes to the comments). I should probably be good to go and probably shouldn’t worry too much about vendoring the actual gem files.

1 Like