From: "Mauricio Fernández" Date: 2003-06-09T02:18:51+09:00 Subject: Re: run-time type evaluation and programming style 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 -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com <|ryan|> I don't use deb u poor man netgod: heh apt-get install task-p0rn