--- nobu.nokada / softhome.net wrote:

> Hi,
> 
> At Tue, 7 Jun 2005 01:00:23 +0900,
> Eric Mahurin wrote in [ruby-talk:144691]:
> > 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})
> 
> I'd posted a feature called `behavior' in [ruby-dev:25772].
> 
>   alphabet = Object.behaving(:[]) {|i|(?a+i).chr}
>   p alphabet[20] #=> "u"
> 
> -- 
> Nobu Nakada

Looks like 2 approaches to doing the same thing.  I do like the
hash interface a little better.  From your code that I read, it
looks like you can do this:

alphabet = Object.new.behaving(:[] => proc{|i|?a+i}, size:
proc{26})

I didn't know about the ":symbol => value" shortcut of "symbol:
value".  Or forgot about it.  Very nice in this situation.

Is there an advantage to having a separate Behavior class as
opposed the solution I had: making a singleton Object directly?

I think having something like this readily available would
promote more (interesting) uses of duck-typing.



		
__________________________________ 
Discover Yahoo! 
Stay in touch with email, IM, photo sharing and more. Check it out! 
http://discover.yahoo.com/stayintouch.html