From: Michael Greenly Date: 2006-08-08T06:31:19+09:00 Subject: Re: How to react on nil or wrong object-type as parameter Yochen Gutmann wrote: > So if I do understand you all correctly, in Ruby you put the responsible > to the caller of a method (eg print_salary()). If you want to test that > method, you test it only with the proper types. The next thing would be > testing the method which includes the call of print_salary() if it calls > this methods with the wrong types of parameter. Is this right? > > One problem in this concept is that if some operations might work on a > parameter-object and the following is not the exception is thrown when > the object is already modified. > > class Bank > @invitations_to_golf > > def handle_client_well client > client.decrease_acoount(200) > client.check_portfolio > @invitations_to_golf.push client > end > > end > > jon_doe = LowBudgetClient.new > big_boss = VeryRichClient.new > big_money = Bank.new > > big_money.handle_client_well big_boss > big_money.handle_client_well jon_doe // poor guy has no portfolio > > So, when the hcw-method is called with jon_doe, he has to pay for better > handling but since he has no portfolio the exception is thrown before he > is entering the golf course. > > One solution might be a exception around the method-body (yes, again > ;-)) and a include an ensure to increase the account again if something > broke. But again, this procedure would fail if there are many modifing > operations. > > Perhaps I am still thinking too Java'ish and these are high theorectical > problems or I still dont get it. > > Yochen Is it by design that you intend to call handle_client with objects that don't have portfolios? Then you need to either check for that in handle_clients or expect the exception and catch it where it's called. If you didn't intend to call handl_client with a non-portfolio baring client than the best thing that can happen is raising an expcetion I'm not really sure I get the conflict you were trying to get to? -- Posted via http://www.ruby-forum.com/.