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
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.
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.