sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Scope of class.initialize

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 877 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.
    • C Offline
      cjthompson
      last edited by

      Does anyone know what scope the "initialize" method of a class is?

      The reason I'm asking is because I accidently put an initialize method in the global scope, and noticed it was called when dcloader.rb and dynamiccomponents.rb loaded.

      I tried some more tests and noticed that some classes executed the initialize method, while some didn't. It seems like classes that were meant to be initialized in ruby didn't execute it, while ones that weren't (Model,Edge,Face,etc.) did.

      Has anyone else experienced this?

      1 Reply Last reply Reply Quote 0
      • A Offline
        avariant
        last edited by

        I know that classes that are created using ruby "C" extensions have the class's 'initialize' method called when a new instance of that class is created (using class.new). I don't know if this follows for classes defined strictly in ruby, but I would assume so. Why your method was called by other rubies, I have no idea, unless it was somehow interpreting your initialize as the initialize for those other classes.

        1 Reply Last reply Reply Quote 0
        • C Offline
          cjthompson
          last edited by

          @avariant said:

          unless it was somehow interpreting your initialize as the initialize for those other classes.

          I think that is part of it, although it looks like the classes original initialize method runs, too. the global initialize method doesn't run when I instantiate pure ruby classes, so I wonder if it is the C extensions that are triggering it.

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            The short answer is don't do it.

            initialize is the method called when new is used to instantiate a new object.

            
            class Person
                def initialize(name)
                    @name = name
                end
            end
            
            jim = Person.new("Jim")
            
            

            Person.new creates an instance (instantiates) of a Person object based on the Person class. You define what happens when new is called in your initialize method.

            If you were to spell out the default behavior of the top-level definition, it would look like this:

            
            
            class Object
              private
              def initialize
              # ...
              end
            end
            
            

            So what happens now is that whenever new is called anywhere, Ruby looks for the initialize method. It looks first in whatever object is self. But if self does not have an initialize method, Ruby looks up the chain of inheritance for initialize and calls the first one it finds. In some cases, it bubbles all the way up to the Object#initialize that you have redefined - because everything in Ruby is a descendant of Object. Or something like that.

            Hi

            1 Reply Last reply Reply Quote 0
            • tbdT Offline
              tbd
              last edited by

              ... also if you def initialize in Ruby console it gives a warning about Object#initialize redefinition can cause infinite loop -> so better class it the initialize method

              SketchUp Ruby Consultant | Podium 1.x developer
              http://plugins.ro

              1 Reply Last reply Reply Quote 0
              • C Offline
                cjthompson
                last edited by

                The reason I was asking was in regards to this post:

                http://forums.sketchucation.com/viewtopic.php?f=180&t=22567

                Is it possible that the bounding box's initialize was interfering with the web dialog's?

                1 Reply Last reply Reply Quote 0
                • J Offline
                  Jim
                  last edited by

                  @cjthompson said:

                  The reason I was asking was in regards to this post:

                  http://forums.sketchucation.com/viewtopic.php?f=180&t=22567

                  Is it possible that the bounding box's initialize was interfering with the web dialog's?

                  I don't think so because I have seen invalid dialogs aldo, and I'm fairly certain I have not redefined initialize at the top-level.

                  I see invalid dialogs through variables that reference a dialog that has been closed. I've added some example code here.

                  Hi

                  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