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!
    ⚠️ Update | Sketchucation Tools 5.0.8 released with licensing improvements and bugfixes Download

    Extending ComponentInstance Class

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 5 Posters 708 Views 5 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.
    • F
      fizgig
      last edited by

      I want to add a new method to the ComponentInstance class. Not sure if it's possible. Here's the basic idea:

      class ComponentInstance
      	def yo
      		UI.messagebox("Tada!")
      	end
      end #class ComponentInstance
      

      I then have try to call the "yo" method on a component instance later and it says the method yo is undeclared.

      Am I missing something?

      1 Reply Last reply
      Reply Quote 0
      • Chris FullmerC
        Chris Fullmer
        last edited by

        Change it to this and it will work:

        class Sketchup;;ComponentInstance
           def yo
              UI.messagebox("Tada!")
           end
        end #class ComponentInstance
        

        You need to specify that the ComponentInstance class is a subclass of the Sketchup class. (did I get my terminology right?)

        Chris

        Lately you've been tan, suspicious for the winter.
        All my Plugins I've written

        1 Reply Last reply
        Reply Quote 0
        • fredo6F
          fredo6
          last edited by

          @chris fullmer said:

          You need to specify that the ComponentInstance class is a subclass of the Sketchup class. (did I get my terminology right?)

          Chris

          Actually "ComponentInstance is a class of the Sketchup module"

          to fizgig
          It is not a good practice to subclass a built-in class, whether Ruby or Sketchup. This could generate clashes between scripts and conflicts in names. Just imagine I do the same, and my script is loaded after yours, then it would be my version prevailing, and your script would not work!
          The best is that you write a method within your namespace (module or class):

          
          module Mymodule
          ....
          def Mymodule.comp_yo(comp)
             UI.messagebox("Tada!")
          end
          
          ....
          end  #module Mymodule
          
          

          Fredo

          1 Reply Last reply
          Reply Quote 0
          • AdamBA
            AdamB
            last edited by

            @unknownuser said:

            It is not a good practice to subclass a built-in class, whether Ruby or Sketchup. This could generate clashes between scripts and conflicts in names. Just imagine I do the same, and my script is loaded after yours, then it would be my version prevailing, and your script would not work!

            I'm sure what Fredo meant to say is that its not good practice to overload (or to some degree) extend a built-in class. Its very good practice to derive from / subclass an existing class to create a specialization you may want.

            Adam

            Developer of LightUp Click for website

            1 Reply Last reply
            Reply Quote 0
            • F
              fizgig
              last edited by

              Understand the concerns. On the way home from work I figured out a way to not have to take this approach but it's good to understand what's going on. Thanks!

              1 Reply Last reply
              Reply Quote 0
              • thomthomT
                thomthom
                last edited by

                @adamb said:

                @unknownuser said:

                It is not a good practice to subclass a built-in class, whether Ruby or Sketchup. This could generate clashes between scripts and conflicts in names. Just imagine I do the same, and my script is loaded after yours, then it would be my version prevailing, and your script would not work!

                I'm sure what Fredo meant to say is that its not good practice to overload (or to some degree) extend a built-in class. Its very good practice to derive from / subclass an existing class to create a specialization you may want.

                Adam

                It's the potential that multiple plugin authors might extend a base class and create conflict.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply
                Reply Quote 0
                • AdamBA
                  AdamB
                  last edited by

                  Hence my point that extending a class has the potential of conflict. Deriving a new class does not.

                  Developer of LightUp Click for website

                  1 Reply Last reply
                  Reply Quote 0
                  • thomthomT
                    thomthom
                    last edited by

                    Ah. Sorry, my bad. Mis-read that.

                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply
                    Reply Quote 0
                    • fredo6F
                      fredo6
                      last edited by

                      @adamb said:

                      I'm sure what Fredo meant to say is that its not good practice to overload (or to some degree) extend a built-in class. Its very good practice to derive from / subclass an existing class to create a specialization you may want.

                      Adam

                      Thanks Adam, this is exactly what I meant.
                      Somehow, the best would be that we restrict the possibility of name clashing only at Module level, which implies that all scripts are alway encapsulated within modules, so that it is easier to control.

                      1 Reply Last reply
                      Reply Quote 0

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better πŸ’—

                      Register Login
                      • 1 / 1
                      • First post
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement