Questions on global, local and instance variables


I understand that global variables are a bad idea- See:

Although I’m not sure why Trimble should care if I use a local vs an instance variable in any given situation.

Strange that these 2 pages do not warn against them:

Also strange that many existing extensions use them:
For example:
$tStrings =“sandbox.strings”)
could easily be called as
tStrings =“sandbox.strings”)
within each method that needs it.

Another frequent example is in menu creation: eg:
$draw_terrain_submenu =“Draw”).add_submenu($tStrings.GetString(“Sandbox”))
Why would such a variable ever be needed outside of the
local context?
Maybe I’m missing something?


We don’t. What prompted to you think that?

Most of our examples are old, like version 6 or older. So they are not the best examples of best practices. We have an open issue to clean them all up.

We also have an open issue to improve our developer pages. There are information that are not always up to date.

There are so many things that stems from the time prior to the time SketchUp was owned by Google and there was very little resources allocated for the API side of things.


Thanks Thomas. As a ruby newbie I just need to have my weak understanding confirmed.

Re: Instance variables: you use them in your golden rule example, but make no mention of local variables.

The extension I’m working on is a single uniquely names module, with several command methods activated by toolbar/menus. I also have several “subroutine methods” called from the command methods. All variables are local. So I’m passing some variables to the subroutine methods and returning them to the command methods. Everything working well so far. I’m not creating any new classes so have no new class methods and no class instances. Why else would I want instance variables? (I haven’t really studied the subject of instance variables yet.)

I was hoping Ruby had a concept of a “module variable” available anywhere inside a module but not outside it. Perhaps not.


Local variables are confined to the RB files executing it - or the given module/method scope. So they never pollute the global environment. In my article I addressed the use of global variables where developers want to share variables across their extensions - where the appropriate thing is to use a instance variable which is local to the namespace instead of global.

That’s where instance variables come into play. You define an instance variable in the module, and then add accessors to them so they can accessed from your child-classes/modules.