From: Jean-Hugues ROBERT Date: 2004-04-26T19:23:42+09:00 Subject: Re: How do you know it quacks like a duck? is there a list of special methods? At 06:44 26/04/2004 +0900, you wrote: >Quoteing djd15@po.cwru.edu, on Tue, Apr 20, 2004 at 05:22:23AM +0900: > > All the methods I can think of in the standard library that do different > > things based on what they're passed, though, actually check the class of > > the object (I think). > >I was reminded by this in a recent post, which said the ruby stdlib had >been changed to use the presence of :read as a method to indicate an >object is IO-like. In that case, the categories I'm aware of are: > ># Class respond_to? > String :to_str > Array :to_ary > IO :read > Symbol :id2name (or is this silly? are only Symbols symbol-like?) > >Are there other "signature" methods? > >Would be useful on the wiki page, maybe under ruby-idioms? > >Cheers, >Sam I use some other ones in my code: xxx.respond_to? :each xxx.respond_to? :call The first one is typically an Enumerable (an existing ruby module) The second one is typically a Callable (Smalltalkish I think, nothing eqv in Ruby). It may prove a good idea to encapsulate this in some methods (of Object) class Object def stringable? ; respond_to? :to_str end def arrayable? ; respond_to? :to_ary end def readable? ; respond_to? :read end def enumerable? ; respond_to? :each end def callable? ; respond_to? :call end end But I suppose this is rather questionable? Another option is to mixim the an appropriate module, as is already the case for Enumerable. Yours, Jean-Hugues ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17