Run-time type evaluation and programming style

Hi everyone,

I have been using Ruby for a couple of months now and have done
several simple utilities in it, and quite frankly I love it. Last
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.

Thanks,

Walt

···

Walter Szewelanczyk
IS Director
M.W. Sewall & CO. email : walter@mwsewall.com
259 Front St. Phone : (207) 442-7994 x 128
Bath, ME 04530 Fax : (207) 443-6284


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:

···

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.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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

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
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap