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

What means "class" here?

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 4 Posters 371 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.
  • H Offline
    honoluludesktop
    last edited by Gábor 31 Jan 2011, 19:07

    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
    • T Offline
      thomthom
      last edited by 31 Jan 2011, 19:16

      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
      • H Offline
        honoluludesktop
        last edited by 31 Jan 2011, 19:47

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

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 1 Feb 2011, 11:36

          @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
          • T Offline
            thomthom
            last edited by 1 Feb 2011, 13:07

            @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
            • D Offline
              Dan Rathbun
              last edited by 1 Feb 2011, 13:41

              @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
              • D Offline
                Dan Rathbun
                last edited by 1 Feb 2011, 13:53

                @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
                • T Offline
                  thomthom
                  last edited by 1 Feb 2011, 13:58

                  @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
                  • D Offline
                    Dan Rathbun
                    last edited by 1 Feb 2011, 14:03

                    @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
                    • T Offline
                      thomthom
                      last edited by 1 Feb 2011, 14:19

                      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 1 Feb 2011, 18:02

                        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
                        1 / 1
                        • First post
                          8/11
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement