From: Anders Borch Date: 2003-06-09T03:56:55+09:00 Subject: Re: run-time type evaluation and programming style Mauricio Fern�ndez wrote: > On Mon, Jun 09, 2003 at 12:55:44AM +0900, walter@mwsewall.com wrote: > >>week I wrote a simple Object persistence manager that would let me do >>simple crud to a db (apparently this is quite popular lately). One >>of the features it does have is that I encode the expected type of >>the data going to and from the database. I do this through the use >>of converter objects. I find that I need to use .class method to >>determine the type of object that I am dealing with, in a case >>statement, and then do what I need to based on that. Most any time >>that I need to use RTTI in Java it is almost certainly a violation of >>the Open/Closed principle (OCP) for that method. But having said >>that by using method overloading in essence I am doing the same >>thing, I am using RTTI to overload the method, but the Java language >>is doing it implicitly, in Ruby I need to do this via direct >>evaluation inside of a method. >> >>I guess what I am asking is this considered generally acceptable in >>Ruby, in Java it would not normally be. I am missing a better way to >>accomplish this. > > > This does probably not apply to your situation, but when I see > case bla.class > when Foo > do_foo > when Bar > do_bar > end > > I always think of doing > > class Foo > def do(context) > ... > end > end > > class Bar > def do(context) > ... > end > end > > and later > > some_context = ... > bla.do(some_context) # let method dispatch decide what is appropriate > > This way you don't have to update 10 case statements if you add a new > class, which I see as a good example of DRY. > > The following is somewhat tangential but interesting nonetheless: > http://www.rubygarden.org/ruby?CoerceExplanation > this is probably a good idea and I should do this some more... A. -- dc -e 4dd*od3*dddn1-89danrn10-dan3+ann6*dan*2*an13dn1+dn2-dn3+5*ddan2/9+an13nap