sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Module-class

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 682 Views 4 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.
    • mariochaM Offline
      mariocha
      last edited by

      HI, guys,
      I need HELP!
      I have written my first method (±70 lines) that works very well.
      Now I want to wrap it in a module and a class.
      (And later a start-operation & commit. And finally a begin-rescue...I guess)
      But adding a - class Name -before everything and -end- as last line breaks it all.
      I'm trying to find good readings about all this.
      Meanwhile here are a few simple lines of it. They give me an "undefined method" error.
      After all that work and studying for weeks, I am so disappointed that these few lines give me a headache !


      Module.rb

      %(#008000)[Mario C.
      Every rule has exceptions, but some.]

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

        You need to call the whole 'path' to the method:
        McF3D::McF3DTools.new()
        AND also add into the initialize() method [which is called by 'new']
        [at the end]
        perceOuv()

        TIG

        1 Reply Last reply Reply Quote 0
        • mariochaM Offline
          mariocha
          last edited by

          @tig said:

          You need to call the whole 'path' to the method:
          McF3D::McF3DTools.new()
          AND also add into the initialize() method [which is called by 'new']
          [at the end]
          perceOuv()

          Many thanks for your answer,
          Heu... but very sorry. Still too abstract for me. 😳
          Tried many ways to implement McF3D::McF3DTools.new(), but cannot figure out where it goes.
          Every scripts I open for reference has a different way of about class.
          very tired.

          %(#008000)[Mario C.
          Every rule has exceptions, but some.]

          1 Reply Last reply Reply Quote 0
          • D Offline
            driven
            last edited by

            I found and still find it confusing, so it good to create a really simple example...

            try each separately or run all at once...

            puts 'hi'
            
            ####################
            
            def my_hi
              puts 'hi'
            end
            my_hi # call
            
            ####################
            
            class My_Hi
            	def my_hi3
            		puts 'hi'
            	end
            end
            My_Hi.new.my_hi3 # call
            
            ####################
            
            module JcB
            	class My_Hi
            		def my_hi4
            			puts 'hi'
            		end # my_hi
            	end # My_Hi
            end # JcB
            JcB;;My_Hi.new.my_hi4 # call
            
            ####################
            
            module JcB
              def self.my_hi5
                puts 'hi'
              end # my_hi
            end # JcB
            JcB;;my_hi5 # call  or  JcB.my_hi5
            
            

            and there are other variations on the theme...

            john

            learn from the mistakes of others, you may not live long enough to make them all yourself...

            1 Reply Last reply Reply Quote 0
            • M Offline
              MSP_Greg
              last edited by

              Mario,

              Glad that helped.

              @mariocha said:

              is the def Initialize a must ?

              Technically, no. But, if there's no need for it, one might ask the question 'should this be a module?' The answer would vary amongst programmers, and isn't dependent on that single criteria.

              [EDIT] I should add that when you create a class, it is a Ruby Class object, which has a :new method, which calls initialize if it exists. Just in case you were wondering...

              Greg

              SUMD SketchUp Misc Docs

              1 Reply Last reply Reply Quote 0
              • mariochaM Offline
                mariocha
                last edited by

                Hey, great ! Now I get it. (slapping my forehead) 😄
                Thanks a lot.
                On to start_operation - commit now. update: that was an easy one.
                Oh btw, is the def Initialize a must ?

                %(#008000)[Mario C.
                Every rule has exceptions, but some.]

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MSP_Greg
                  last edited by

                  Mario,

                  This might help (I reformatted your code a bit), see the last couple of lines --

                  
                  # require 'sketchup.rb'   # why?  not needed for this code
                  module McF3D
                    class McF3DTools 
                      def initialize
                        @mod = Sketchup.active_model  # Open model
                        @ent = @mod.entities          # All entities in model
                        @sel = @mod.selection         # Current selection
                      end
                      def perceOuv
                        unless @sel[0].is_a?(Sketchup;;ComponentInstance) &&
                            @sel[0].definition.behavior.cuts_opening?
                          UI.messagebox "Sélectionnez un composant F3D !"
                        end  
                      end 
                    end # class McF3DTools
                  end # module McF3D
                  obj = McF3D;;McF3DTools.new()
                  obj.perceOuv()
                   
                  

                  The topic of modules & classes in programming is very complex. Conversely, what most people writing a SketchUp extension need to do is a very small subset of that topic.

                  HTH,

                  Greg

                  SUMD SketchUp Misc Docs

                  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