What means "class" here?
-
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> -
Had to look up "inheritance" to get it. Thanks.
-
@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]
-
@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 == -
@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.classand.is_a?both have the same number of characters.)BUT.. your reply just made me realize something I missed.
object.class==ClassNameis actually calling 2 Ruby methods, whereas:
object.kind_of? ClassNameis only calling 1 Ruby method, so ....
in long loops,kind_of?()is bound to be faster(the fastest of the three.) -
@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::ComponentInstanceThis is faster than
.is_a?()orkind_of?()because:
EDIT: not so.. I forgot that both.class()AND.==()are both method calls, andkind_of?()is only one call so it's faster. (see posts further down.)- class
Classinherits it's==()method from classObject, via classModule. TheObject.==()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::ComponentInstanceThis 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
Drawingelementsubclasses 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 returnstrueonly for objetcs like faces, edges, curves, etc. - class
-
@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.782cclass - 0.875c`
Second test:
` is_a? - 16.297c
kind_of? - 16.141cclass - 18.703c`
The odd bit is the .class call being slower in the second test. I credit the difference to other system variations.
Sinceis_a?andkind_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.
-
@thomthom said:
The odd bit is the
.classcall 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==ClassNameis actually calling 2 Ruby methods, whereas:
object.kind_of? ClassNameis only calling 1 Ruby method,
so ....
in long loops,kind_of?()is bound to be faster(the fastest of the three.) -
That's a good point. I still keep forgetting that
==as method.==. -
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>
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 LoginAdvertisement