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!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    What means "class" here?

    Scheduled Pinned Locked Moved Developers' Forum
    11 Posts 4 Posters 487 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by Gábor

      What's the difference if any?

      ss = model.selection
      ss.each do |e|
        if e.class == Sketchup;;ComponentInstance
          #other stuff
        end
      end
      
      ss = model.selection
      ss.each do |e|
        if e.is_a? Sketchup;;ComponentInstance
          #other stuff
        ebd
      end
      

      tt, or anyone else, how do I get color in my code? "Font colour" doesn't seem to do it.

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

        is_a? checks for inheritance as well.

        <span class="syntaxdefault"><br />class Foo<br />end<br /><br />class Bar </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> Foo<br />end<br /><br />f </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Foo</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxdefault">b </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Bar</span><span class="syntaxkeyword">.new<br /><br /></span><span class="syntaxdefault">f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">class </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> Foo </span><span class="syntaxcomment"># => true<br /></span><span class="syntaxdefault">b</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">class </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> Foo </span><span class="syntaxcomment"># => false<br /></span><span class="syntaxdefault">b</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">class </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> Bar </span><span class="syntaxcomment"># => true<br /><br /></span><span class="syntaxdefault">f</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Foo</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># => true<br /></span><span class="syntaxdefault">b</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Foo</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># => true<br /></span><span class="syntaxdefault">b</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Bar</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxcomment"># => true<br /></span><span class="syntaxdefault"> </span>
        

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

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          Had to look up "inheritance" to get it. Thanks.

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

            @hd said:

            tt, or anyone else, how do I get color in my code?

            We are cheating, the color lexer for php is built-in to phpBB, so we are change the 1st code tag:

            It does not properly color Ruby but ... we are impatient. 😛

            [code=php]

            I'm not here much anymore.

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

              @dan rathbun said:

              This is a wee bit slower because on the C side:[list]

              Results seems negligible though: http://forums.sketchucation.com/viewtopic.php?f=323&t=19576&st=0&sk=t&sd=a&start=15#p166698
              The difference between the test of the same method differs just as must as the difference between .is_a? and .class.
              Maybe performance is more noticeable if the objects has higher levels of inheritance, but there seem to be no practical savings in .class ==

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

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

                @thomthom said:

                Results seems negligible though: http://forums.sketchucation.com/viewtopic.php?f=323&t=19576&st=0&sk=t&sd=a&start=15#p166698

                Well ... compiled C is really fast!! We are talking about milliseconds here.

                Nice .. but. How many times did you run the tests ?
                I've found it really takes a minimum of 8 times (saving the results, then taking the mean of the total.)

                @thomthom said:

                The difference between the test of the same method differs just as must as the difference between .is_a? and .class.

                I don't see that in the numbers.. I see that using the alias .is_a? takes a wee bit more time that calling .kind_of? directly.

                @thomthom said:

                Maybe performance is more noticeable if the objects has higher levels of inheritance, but there seem to be no practical savings in .class ==

                I would agree with 'higher levels' as the C while loop must eat time...

                The main reason I do it (and encourage it,) is for readability. If your testing for class equality, I use the == it's only 2 more characters (as .class and .is_a? both have the same number of characters.)

                BUT.. your reply just made me realize something I missed.
                object.class==ClassName is actually calling 2 Ruby methods, whereas:
                object.kind_of? ClassName is only calling 1 Ruby method, so ....
                in long loops, kind_of?() is bound to be faster(the fastest of the three.)

                I'm not here much anymore.

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

                  @honoluludesktop said:

                  What's the difference if any?


                  .is_a?() is a bit misleading, it is an alias for .kind_of?() (which I prefer to use instead.)


                  if e.class == Sketchup::ComponentInstance

                  This is faster than .is_a?() or kind_of?() because:
                  EDIT: not so.. I forgot that both .class() AND .==() are both method calls, and kind_of?() is only one call so it's faster. (see posts further down.)

                  • class Class inherits it's ==() method from class Object, via class Module. The Object.==() method, is implemented in C, as a simple if statement:
                  static VALUE
                  rb_obj_equal(obj1, obj2)
                      VALUE obj1, obj2;
                  {
                      if (obj1 == obj2) return Qtrue;
                      return Qfalse;
                  }
                  

                  if e.is_a? Sketchup::ComponentInstance

                  This is a wee bit slower because on the C side:

                  • it has to go through a switch statement for typechecking, then through a while loop
                  VALUE
                  rb_obj_is_kind_of(obj, c)
                      VALUE obj, c;
                  {
                      VALUE cl = CLASS_OF(obj);
                  
                      switch (TYPE(c)) {
                        case T_MODULE;
                        case T_CLASS;
                        case T_ICLASS;
                          break;
                  
                        default;
                          rb_raise(rb_eTypeError, "class or module required");
                      }
                  
                      while (cl) {
                          if (cl == c || RCLASS(cl)->m_tbl == RCLASS(c)->m_tbl)
                              return Qtrue;
                          cl = RCLASS(cl)->super;
                      }
                      return Qfalse;
                  }
                  
                  • Since all Sketchup's Drawingelement subclasses have NO subclasses of their own, using .is_a?() or .kind_of?() is pointless, IF the argument is the Drawingelement subclass your testing for.

                  So when should you use .is_a?() or .kind_of?() ??
                  EDIT: Always when iterating, especially the model or other large collections, use kind_of? or is_a? (see posts further down.)

                  When iterating the objects in a model, if you are only interested in those "kind of" objects that are drawing elements, and you wish to ignore such objects as layers, views, pages, shadowinfo, etc.; then you would use:
                  if e.kind_of?( Sketchup::Drawingelement )
                  which returns true only for objetcs like faces, edges, curves, etc.

                  I'm not here much anymore.

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

                    @dan rathbun said:

                    Nice .. but. How many times did you run the tests ?

                    I ran them on models I had, you see the model status under the data results.

                    @dan rathbun said:

                    I don't see that in the numbers.. I see that using the alias .is_a? takes a wee bit more time that calling .kind_of? directly.

                    First test:
                    ` is_a? - 0.89c
                    kind_of? - 0.782c

                    class - 0.875c`

                    Second test:
                    ` is_a? - 16.297c
                    kind_of? - 16.141c

                    class - 18.703c`

                    The odd bit is the .class call being slower in the second test. I credit the difference to other system variations.
                    Since is_a? and kind_of? are aliases they should be the same. (If we tested spherical chickens in vacuum. 😉 )

                    @dan rathbun said:

                    I've found it really takes a minimum of 8 times (saving the results, then taking the mean of the total.)

                    Yea - I didn't do that.

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

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

                      @thomthom said:

                      The odd bit is the .class call being slower in the second test. I credit the difference to other system variations.

                      Nope ... not variables.

                      Its actually *object*.class().==( *argument* )

                      @dan rathbun said:

                      BUT.. your reply just made me realize something I missed.

                      object.class==ClassName is actually calling 2 Ruby methods, whereas:
                      object.kind_of? ClassName is only calling 1 Ruby method,
                      so ....
                      in long loops, kind_of?() is bound to be faster(the fastest of the three.)

                      I'm not here much anymore.

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

                        That's a good point. I still keep forgetting that == as method .==.

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

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

                          Don't forget instance_of? when only looking for a specific class without regard to inheritance. As in

                          <span class="syntaxdefault"><br />ss </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">selection<br />ss</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">  if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">instance_of</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance<br />    </span><span class="syntaxcomment">#other stuff<br /></span><span class="syntaxdefault">  end<br />end</span>
                          
                          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