Greetings...

I'm continuing my learn-to-write-OO-Ruby journey. Had a recent bad 
experience trying to convert a complex method to a class. The method 
gets a lot of use in my program, and each time it needs to know a lot 
about the environment outside itself. I found myself having to write a 
ton of instance variable data into the class instance to get it to do 
its job, before I called it each time, then read a few more back out to 
get the results. It was awful. What had been a one line call was now 
about 14 lines of code. Ack! I gave up and converted it back to a 
method, which simply makes more sense. I could not find any 
"class-magic" in this experience - just a lot of locked doors.

I now have three questions. I have read a number of people's accounts of 
what classes are and how you build them, etc., etc., and no one seems to 
address these matters at all well (or else I missed it):

1. HOW do you use a class?

I was assuming that since I couldn't pass data to an instance, after 
creation, I have no option but to write data into its instance vars as 
needed. Sometimes, it seems there simply is no other option.

But, is it approved practice to do something like

junk = MyClass.new( var_1...var_n ).mymethod

which creates an instance and calls a method which leaves its results in 
some instance vars., then simple read the results with...but how? 
there's no instance object! (Not that I can find, anyway.) That seems to 
leave me only with this possibility:

myclass = MyClass.newMyClass.new( var_1...var_n )
myclass.mymethod
varA = myclass.var_whatever
etc....until I have all my results back out of the instance.

Compared to a simple method call, this seems designed to make me crazy 
quickly. Is there a better way?

Then, to call the instance again, I have to write new data into its 
instance vars. This simply looks like nonsense, unless one really needs 
to have the encapsulation that an instance offers. Am I missing 
something? Is this just the facts of life when using classes?

2. Is it accepted practice to simply create a new instance every time 
the class is needed, thus setting the instance's state once, using it 
with one or more method calls, then moving on to the next new instance? 
It occurs to me that maybe Ruby's garbage collection would sweep the old 
instance right up, knowing it won't be used again, but I don't know.

My nightmare case is a class which operates on an input record, but 
differently each time, depending upon a number of factors in the 
environment outside the class. I just can't see a graceful way to do 
this. I'm struggling to see why I do OO programming at all in this case.

3. Finally, I'm still struggling with the "when do I make something a 
class?" question. I'm surprised that this question is so unimportant or 
its answer so obvious that no one much addresses it. Dave Thomas, in his 
3rd ed. (I just upgraded, and its really nice!) finally gives two 
sentences to the matter, which is way more than I can find anywhere 
else: "Whenever you√”e designing OO systems, a good first step is to 
identify the things you√”e dealing with. Typically each type of thing 
becomes a class in your final program, and the things themselves are 
instances of these classes."(p. 59)

I've been thinking only in terms of functions, things my program does, 
and not things it works on or with. Both are relevant, clearly, and I'm 
now out on a hunt for "things" that are more than functions. Maybe that 
will help.

Anyone have any additional advice about "when to make something a 
class?" The principle reasons I see are to achieve scope closure, 
persistent state, and object duplication (multiple instances). Did I 
miss anything important?

I come to the list with these questions only after days of struggle, 
with lots of code, some of it a rather nasty experience (errors I've 
never ever seen before!!!). The questions above are the ones I simply 
have not been able to resolve, and any help offered will be gratefully 
received.

Thanks,

Tom


The easier one (probably):

-- 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc / tomcloyd.com >> (email)
<< TomCloyd.com >> (website) 
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~