From: dblack@... Date: 2006-03-27T22:00:13+09:00 Subject: Re: Duck typing bunches of strings Hi -- On Mon, 27 Mar 2006, listrecv@gmail.com wrote: > In my quest to master the fine art of duck typing / overloading, I've > run into this probem: > > How can I identify a collection? > The problem is: String mixes in Enumerable, although it ain't a > collection. > > My method wants to act differently if it's given a collection, then if > it's just given a single object. I would check .respond_to? :each - > but String does, unfortunately. > > Currently, I'm forced to do .kind_of? Array - but this ain't duck > typing. > > Any ideas? I'd like to separate Array, Hash, Set, etc. etc. from > single member objects. If you really want to do duck typing in your method, then you would need to prepare the objects in advance, by having them learn a particular method, probably via an extend operation. Then your method can just ask each object to execute its version of that method. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black