Passing the value of a variable between a module and an Observer class

Hello,

After a period of break I resume my learning in Ruby.

I am currently working with observers to perform some actions after importing a new entity to the SketchUp entity collection.

I want to transmit the “number” variable present in the “Test_A” module to the “Observer” class.

How can I do this?

module Plugin
  class Observateur < Sketchup::EntitiesObserver
    def onElementAdded(entities, entity)  
      if entity.class == Sketchup::ComponentInstance
	    p "NAME = #{entity.definition.name} = #{number}"
      end
	  entities.remove_observer(self)
    end
  end  
  module Teste_A  
    def self.add_observateur()
      Sketchup.active_model.entities.add_observer(Observateur.new() )
    end
	def self.import_01
	  number = "01"
	  add_observateur()
	end
	import_01
  end
end

Thank you in advance for your help.

module JulienDufren
  module Plugin

    class Observateur < Sketchup::EntitiesObserver

      attr_accessor :number

      def initialize(num)
        super
        @number = num
      end

      def onElementAdded(entities, entity)  
        if entity.is_a?(Sketchup::ComponentInstance)
          entity.name = "#{entity.definition.name} : #{number()}"
          p entity.name
        end
        entities.remove_observer(self)
      end

    end

    module Teste_A
      def self.add_observateur(num)
        Sketchup.active_model.entities.add_observer(
          Observateur.new(num)
        )
      end

      def self.import_01
        number = "01"
        add_observateur(number)
      end

      import_01()
    end

  end
end
2 Likes

Thank you very much Dan!
Your example works perfectly.

1 Like