From: Robert Klemme Date: 2005-06-07T02:31:46+09:00 Subject: Re: making a duck Eric Mahurin wrote: > 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}) Why aren't you satisfied with class Alpha def [](i) ?a+i end def size() 26 end end alphabet = Alpha.new or alphabet = Object.new def alphabet.[](i) ?a+i end def alphabet.size() 26 end You can even squeeze that on one line if you feel the need for it. I mean, you don't generate those methods dynamically or get them from somewhere else so why not just use the std approach? Kind regards robert