From: Eric Mahurin Date: 2005-06-07T01:00:23+09:00 Subject: making a duck Regarding duck-typing... Is there an easy way make a "duck"? i.e. an object that responds to enough methods to be an acceptable argument to a certain methods. For example, if I have a method that takes aString and uses the #size and #[i] methods, I could pass it something that looked just enough like a String to work: alphabet = Object.duck(:[],proc{|i|?a+i},:size,proc{26}) The implementation I came up with is this: class Object def self.duck(*name_proc) duck = self.new duckclass = (class << duck;self;end) while not name_proc.empty? name,proc = name_proc.slice!(0,2) duckclass.send(:define_method,name,&proc) end duck end end Is there another way to do this? With all of the talk about duck-typing, there would already be something out there to do this. __________________________________ Discover Yahoo! Stay in touch with email, IM, photo sharing and more. Check it out! http://discover.yahoo.com/stayintouch.html