Simple read variable question

In my code I’d like to turn some observers off and reset them if they were on in the first place.
I’ve figured out that by checking the “singleton frame change observer object” SPY - I get the following data where if @oid is non nil, the observer is running. But with my rudimentary ruby skills, I don’t know how to parse & save the variable @oid

Could someone explain how to save the @oid variable?

Thank you

command prompt accessing the observer status:

SAA::SAA_sceneOptionsChangerObserver::SPY
=> #<SAA::SAA_sceneOptionsChangerObserver::FrameChangeObserver:0x00000278031ef320 @current_page=#<Sketchup::Page:0x000002b920b80548>, @oid=nil, @check=0, @prompt="Click to enable Scene Change Components.">
SAA::SAA_sceneOptionsChangerObserver::SPY
=> #<SAA::SAA_sceneOptionsChangerObserver::FrameChangeObserver:0x00000278031ef320 @current_page=#<Sketchup::Page:0x000002b920b80548>, @oid=34, @check=8, @prompt="Click to disable Scene Change Components.">

my pseudo code:

# save state of observer
@orig_observerState = (something) @oid

# (do stuff)

# at end reset original state of observer
if !@orig_observerState.nil?
  SAA::SAA_sceneOptionsChangerObserver::SPY::restart
end

The observer code comes from scenes_v2.rb from this post: Update to "scenes.rb" example (from the old API blog)

Wow … 10 going on 11 years for that old topic thread.

I’ve been waiting like that long to hope the SketchUp Extensibility Team would allow version 3 to be posted.

It may take awhile for me to look into that old code and respond.

1 Like

Thank you. I think in this case I’m just trying to learn the basics of ruby and how to extract one part of multi-part data returned by this command:

SAA::SAA_sceneOptionsChangerObserver::SPY

which returned this data, for which I’ve broken the lines at the commas.

=> #<SAA::SAA_sceneOptionsChangerObserver::FrameChangeObserver:0x0000025cbe4a06d8 @check=8,
@prompt="Click to disable Scene Change Components.",
@current_page=#<Sketchup::Page:0x0000029d9867cc98>,
@oid=21>

I can see that @oid=21 means the observer is active, otherwise it would be nil.

Okay if you followed instructions then SAA is your toplevel namespace module.
It looks like you’ve inserted a submodule SAA::SAA_sceneOptionsChangerObserver ?

Otherwise the SPY instance should be an instance of the FrameChangeObserver class.
This custom class defines some instance methods, in addition to those inherited by it’s ancestor Ruby core classes.
You can see what an instance object’s ancestors are via: my_object.class.ancestors

If you look at the v2 code you should see that I defined an instance getter method for the observer’s ID at line 180 of the original file.

So if you have the SPY reference just call SPY.observer_id

It is already saved within the SPY instance itself. The whole purpose of instance variables in Ruby is to save the state of the instance object’s properties.

But if you MUST save a reference to the id then:

  current_id = SPY.observer_id

Note that in Ruby variables are really reference pointers to other objects.

In this case, using the above assignment, current_id would be a reference to the integer the @oid points to, at that moment when the assignment is made. Integer objects are immediate objects, meaning there is a finite set of integers, so there is only one object 21. You can get information about this unique object, but you cannot change it.

You should be referring to my Ruby Learning lists:

What you describe is called object or type introspection & reflection. This is built into Ruby.

To examine Ruby objects you need to know about the introspection and reflection methods that are inherited from BasicObject, Kernel, Object, Module and Class.

You need to refer to the Ruby Core Documentation:

The SketchUp API only defines it’s own modules and classes, and in a few places adds some methods to core classes like Array and the Numeric class.

But just as an example using core introspection:

current_id = SPY.instance_variable_get(:@oid)

The instance_variable_get method is inherited from Object.

2 Likes

Thank you! That was an amazingly detailed response. I missed that I could call observer_id (line 180), even though I figured out I could call pause and restart.

I really appreciate the time you took to explain. I know I should find the time to learn Ruby formally through documentation and books. My use of Sketchup is sporadic, and writing scripts even less frequent. When inspiration hits I try and cobble a solution which leads to some learning but I’m stumbling without the foundation.

1 Like

Well, again the foundations can be reached through the Ruby Resources list that is pinned to this category. If you have a tablet, etc., you could do some reading whilst sitting “on the pot”. Some of the books are downloadable as PDFs.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.