tag:blogger.com,1999:blog-9905395.post1075645083184048411..comments2023-06-13T02:09:57.338-06:00Comments on David Rupp's Blog: Regarding Ruby, instance_of?, kind_of?, and ===David Rupphttp://www.blogger.com/profile/16410820024654313029noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-9905395.post-34531002817321100632008-08-16T15:33:00.000-06:002008-08-16T15:33:00.000-06:00@farrel: Interesting stuff. I'll look forward to t...@farrel: Interesting stuff. I'll look forward to the other installments in the series.David Rupphttps://www.blogger.com/profile/16410820024654313029noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-79633258191913615812008-08-16T15:26:00.000-06:002008-08-16T15:26:00.000-06:00Yeah -- according to the docs is_a? is an alias fo...Yeah -- according to the docs is_a? is an alias for kind_of? (or the other way around, I guess). I'd forgotten about that one too.David Rupphttps://www.blogger.com/profile/16410820024654313029noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-87018246148534398222008-08-16T12:44:00.000-06:002008-08-16T12:44:00.000-06:00@davidActually, it wasn't until reading your post ...@david<BR/><BR/>Actually, it wasn't until reading your post that I thought about those methods and their differences. For all my Ruby programming days, I've been using is_a?.<BR/><BR/>But it's the case statement that's an especially powerful use of ===.Avik Dashttps://www.blogger.com/profile/17872698995240619570noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-2375152888982112612008-08-16T09:18:00.000-06:002008-08-16T09:18:00.000-06:00@avik das (first comment): Right. So it's not that...@avik das (first comment): Right. So it's not that I expected those two methods to do the same thing. I knew about instance_of? and expected it to do the same thing as Java's instanceof. I didn't know about kind_of? at all. <BR/><BR/>My fault, I know, for thinking like a Java programmer. This is exactly why I'm making myself read more idiomatic Ruby.David Rupphttps://www.blogger.com/profile/16410820024654313029noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-27260945988945051282008-08-16T05:12:00.000-06:002008-08-16T05:12:00.000-06:00=== is actually pretty powerful and woefully under...=== is actually pretty powerful and woefully underused. I'm actually writing some posts about using === in Ruby. Here's the first one: <A HREF="http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/" REL="nofollow">Unlocking The Power Of Case Equality: Proc#===</A>Unknownhttps://www.blogger.com/profile/09751737895601586515noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-68151610026004990632008-08-16T00:44:00.000-06:002008-08-16T00:44:00.000-06:00Sorry for the double post, but I forgot to mention...Sorry for the double post, but I forgot to mention something: === is used by the case expression no matter what the objects. In the case of a Class, like Fixnum, it's useful as an alias to kind_of? so you can do the following:<BR/><BR/>case obj # test obj's type<BR/> when Fixnum then treat_as_number(obj)<BR/> when String then treat_as_string(obj)<BR/>endAvik Dashttps://www.blogger.com/profile/17872698995240619570noreply@blogger.comtag:blogger.com,1999:blog-9905395.post-21392397468159801642008-08-16T00:35:00.000-06:002008-08-16T00:35:00.000-06:00instance_of? doesn't return true for superclasses,...instance_of? doesn't return true for superclasses, but kind_of? does. They're <I>different</I> methods.<BR/><BR/>=== is also a method, and for Classes, and it's basically an alias for is_a?, which is an alias for kind_of?.Avik Dashttps://www.blogger.com/profile/17872698995240619570noreply@blogger.com