From: "Weirich, James" Date: 2003-11-22T04:06:01+09:00 Subject: Re: "stereotyping" (was: Re: Strong Typing (Re: Managing metadata about attribute types) ) > This is a wonderful idea. Let me restate it to make sure I > understand it. Just as you can do the following: > > class Person > def greet(other_person) > other_person.to do > say "hello" > end > end > end > > class Person > def some_other_method > end > end > > > ...(I've defined class Person somewhere, but I can always open up the > class again and define something else in it): it would be > wonderful if you can *either* inline it *or* make it external in > a similar fashion. [...] > I think the ability to do this would make the whole idea a lot more > palatable to a lot of us. I've been thinking about DbC stuff for some time and something in Matz Ruby 2 talk last Saturday sparked an idea along these lines ... class Person def greet(other_person) marry(other_person) end end Then specify the contract in a separate file ... class Person def greet:pre(other_person) assert ! self.married? assert ! other_person.married? end def greet:post(other_person) assert other_person.married? assert self.married? assert_equal self, other_person.spouse assert_equal other_person, self.spouse end end An aside: Unfortunately, the post method doesn't have access to the result returned from the function. In this case it doesn't matter, but many post conditions assert something interesting about the result. I guess I would have to use a wrap method for that. -- -- Jim Weirich / Compuware -- FWP Capture Services -- Phone: 859-386-8855