From: itsme213 Date: 2005-01-14T03:06:15+09:00 Subject: Re: Duck Typing as Pattern Matching "trans. (T. Onoma)" wrote > | My claim is this: If you redefine #method_missing so your object handles > | x.foo as the caller would expect, then you _should_ define > | x.respond_to?(:foo) to return true. I do not claim that Ruby should > | prohibit code that does not do this. > > I wonder if method missing would be better if these two "tiers" were built in > to it. This would require dual "blocks" so I'm not sure how to notate it but: > > def method_missing(meth, *args) > upon meth == :create_modify > # do whatever > upon ... > # ... > end > > Then #respond_to? can utilize the "upon" block to determine responses > correctly. Not sure what "upon" is. If your intent is to associated distinct method_missing handler procs with selectors, so that Object#respond_to? can utilize this properly, it can be done in many ways. e.g. Object could provide a register_method_missing_handler: class MyClass register_method_missing_handler :selector, { |*args| handler } end Then Object#respond_to? could utilize the registered selectors. One difficulty would be generic handlers (e.g. a proxy handles anything its real target does, OpenStruct handles any x() and x(y)), so the "associated selectors" would need to include something like pattern matching or wildcards. Oh, oh, ... does that mean we are back to duck typing based on pattern matching ? ;-) A separate, but related, issue is that I _really_ think anywhere selectors are passed around in 2.0 they should uniformly include information about method arity, keyword arg names, and block params (at least optionally). If 2.0 does selector namespaces, then selectors have to include namespace info as well. x.respond_to?(:x) where :x is in namespace-1 x.respond_to?(x) is :x is in namespace-2. I think selectors > > | I am interested in your thoughts on this claim. > | > | p.s. I know it is possible to redefine #send so that x.send :foo works, but > | x.foo does not. I think this a practice to be actively discouraged. But > | let's leave that out for now. > > Redefining #send is a very bad idea. IMHO That's one of those methods I think > should be off limits and return an error if you try to redefine it. > > T. > > >