From: Bill Barnhill Date: 2006-03-14T23:55:31+09:00 Subject: Re: [RCR] abstract method in Ruby ------=_Part_3836_3654414.1142348114547 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Good points both, thanks. If I have the constraint that objects within a certain group must be able t= o respond to the methods that make up behavior X, then what would be the best way to ensure that? If my group is represented by a collection then I can check for msg responding on adding to the collection, but if I have a large number of methods to check, and several different collections, then I'd rather not have respond to code in each collection's insertion method. If I declare a delegate at beginning of class with required methods defined= , then as I def those methods later in class the delegate methods will get replaced, right? Is that then the Ruby way to do this? The behavior I am aiming for is essentially a 'kind_of?' for duck typing, which as you pointed out is mixing two opposite philosophies. But I can se= e usefulness of something like pattern_of?..ahh, maybe a light bulb is going off here. Would this then be the ruby way: .. patterned_obj.pattern_of?(pattern_obj) that does the following .. Gets symbols for public methods on pattern_obj .. Check to see that patterned_obj responds to all these methods Still doesn't check for param count though, but it sounds like that may be = a good thing, as Ruby makes nice use of different param types and counts..example being Hash.each. --Bill Bill Barnhill i-name: xri://=3DBill.Barnhill (For more info: http://2idi.com/grs/index.php?referral_code=3Dcommunitivity ) On 3/14/06, dblack@wobblini.net wrote: > > Hi -- > > On Tue, 14 Mar 2006, Bill Barnhill wrote: > > > Oops, my apologies, forgot an end in example. > > Should read > > class B < A > > def initialize > > @a =3D "foo" > > end > > end > > > > Another though on this is that it would allow us to define message > > interfaces (aka duck-typing prototypes). > > I fear that "duck-typing prototype" is a contradiction in terms. > With duck typing, you're dealing with an object: it's all about the > moment you send the message to the object, and the *absence* of > concern about where that object came from, what its pedigree is, and > so forth. "Message interfaces" suggests a higher-level clustering of > behaviors. > > > On 3/14/06, Bill Barnhill wrote: > >> > >> Hmm, just some thoughts. > >> I think the abstract method idea nice in concept, but implementation > >> perhaps too Java-ish. > >> > >> If I get a chance later this week I'll code this, but for now here's > how > >> I'd envision something like this within Ruby code: > >> > >> class A > >> should_respond_to_behavior #..options hash describing what to do > if > >> msg not > >> # responded to properly > >> goes here, > >> # nice to support YAML > as > >> well.. > >> should_respond_to :foo, :bar =3D> {}, :baz =3D> {:param_count =3D>= 3} > >> end > >> > >> class B < A > >> def initialize > >> @a =3D "foo" > >> end > >> > >> B.new > >> =3Dbegin > >> At this point the class a checks responses: > >> .. it expects a response to messages :foo and :bar with any or no > params > >> .. it expects a response to baz with three params > >> .. raises exception if either check fails, or logs, depending on > >> configuraiton > > Having a class police the behavior of its instances after they've > already been created strikes me as being at odds with some of the > basic conditions of Ruby runtime -- namely, that objects can change > and aren't constrained by the circumstances of their creation. I > suspect it would also be likely to discourage duck typing. > > > 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 > > ------=_Part_3836_3654414.1142348114547--