From: "David A. Black" Date: 2007-10-23T02:26:43+09:00 Subject: Re: Conflicts between using respond_to? and extending Ruby Hi -- On Tue, 23 Oct 2007, ara.t.howard wrote: > > On Oct 22, 2007, at 10:39 AM, David A. Black wrote: > >> The distinction I see is proximity to the object. respond_to? is >> asking the object about its capabilities, whereas kind_of? is asking >> it about its ancestry. So respond_to? is a tighter fit with the object >> as it is at a given point in runtime. > > it all depends - consider the case of trying to tell wether or not a hash, > array, or rbtree is passed to a method. you can't use 'respond_to?("[]")', > 'respond_to?("size")', etc. you can sometimes pick the 'right' method but, > other times you cannot and I don't think there even is a right method for this purpose; I don't think respond_to? ever enters into the picture when you want to know the class of an object. It doesn't convey that information. > case obj > when Array > ... > > is safer and more accurate. i think it really depends on the use case - i > personally use both. It does indeed depend; they do totally different things. I was describing the differences, specifically in the context of duck typing (or alternatives to duck typing): respond_to? gives you information about an object at a given moment, and kind_of? tells you about its ancestry. David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing with Rails, Edison, NJ, November 6-9 * Advancing with Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!