Ruby Observer Existence Check  Topic is solved

Ruby Observer Existence Check

Postby hank » Sat Apr 22, 2017 4:29 pm

Is there any way to check to see if an entity already has an observer attached to it?

I am using:
Code: Select all
class MyInstanceObserver < Sketchup::InstanceObserver
  def onChangeEntity(entity)
     #puts "onChangeEntity: #{entity}"
  end
end


and then

Code: Select all
my_openings = ent.grep(Sketchup::ComponentInstance).find_all {|oi| oi.definition.name.include?('OPENING') }
  my_openings.each do |oi|
    next unless oi.valid?
  oi.add_observer(MyInstanceObserver.new)
end


But would prefer not to blindly attached an observer always. This loop will have been run previously.
0

hank 
 

Re: Ruby Observer Existence Check  Topic is solved

Postby TIG » Sat Apr 22, 2017 5:02 pm

How about doing it this way.
Outside of any of your methods...

@@obs = MyInstanceObserver.new() unless @@obs

That only runs once and its reference can then be used thus:

oi.remove_observer(@@obs)
oi.add_observer(@@obs)


Attempting to remove a non-existent observer does NOT raise an error.
So in this way just one instance of the observer is only ever attached to an instance.
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby Observer Existence Check

Postby hank » Sat Apr 22, 2017 8:40 pm

Thanks TIG. I though @@ was for classes???

I got an "uninitialized class" error until changed @@obs to @obs

Your solution worked perfect though!
0

hank 
 

Re: Ruby Observer Existence Check

Postby TIG » Sat Apr 22, 2017 9:54 pm

You usually set @@obs = nil inside a class [outside of any method].
Then within one of that class's methods [like initialize()] you can set it to be a proper reference - but just the once.
In a class it needs to be @@obs = ... to persist across instances of the class.
A @obs = ... in a class only exists during that one instance of the class.

In my example I set it only if it's not already set [i.e. it is nil].

You can set @obs = ... within a module, outside of any of its methods and that then persists thereafter.

Since I didn't have your whole code I guessed... :?
1
TIG
User avatar
TIG 
Global Moderator
 

Re: Ruby Observer Existence Check

Postby hank » Mon Apr 24, 2017 2:05 am

Thanks TIG. Have not gotten very advanced with Classes etc. so just a simple module. Appreciate the explanation.
0

hank 
 

Re: Ruby Observer Existence Check

Postby Dan Rathbun » Tue Apr 25, 2017 4:26 pm

TIG wrote:You can set @obs = ... within a module, outside of any of its methods and that then persists thereafter.

This is true because, a module is an instance of class Module.

hank wrote:Have not gotten very advanced with Classes etc. so just a simple module.

The Class class is the direct child class of class Module. So classes inherit all of Module's functionality, and get a little bit of their own (ie, a few extra methods and the ability to have many instance copies of themselves, that can hold their own instance variable values.)
1
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    cron

    Who is online

    Users browsing this forum: No registered users and 5 guests

    Visit our sponsors: