sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Ruby Observer Existence Check

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 3 Posters 1.7k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      hank
      last edited by

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

      I am using:

      
      class MyInstanceObserver < Sketchup;;InstanceObserver
        def onChangeEntity(entity)
      	  #puts "onChangeEntity; #{entity}"
        end
      end
      
      

      and then

      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.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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.

        TIG

        1 Reply Last reply Reply Quote 0
        • H Offline
          hank
          last edited by

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

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

          Your solution worked perfect though!

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            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... πŸ˜•

            TIG

            1 Reply Last reply Reply Quote 0
            • H Offline
              hank
              last edited by

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

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                @tig said:

                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 said:

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

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement