From: "David A. Black" Date: 2009-01-29T11:48:19+09:00 Subject: Re: proper use of classes Hi -- On Thu, 29 Jan 2009, Tom Cloyd wrote: > 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. You can pass data to an instance: instance.some_method(data) > 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! What you've written here is similar to: upstring = String.new("David").upcase or something like that, and it's perfectly legit, though you might also want to grab the object separately: string = String.new("David") upstring = string.upcase > (Not that I can find, anyway.) That seems to leave me only > with this possibility: > > myclass = MyClass.newMyClass.new( var_1...var_n ) I don't quite get that line. What's newMyClass? (Or maybe it's garbling again between our machines?) > 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? I'm not sure I'm following. I guess the short answer is that there's a ton of different patterns you can follow, depending on what you need to do. > 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. Normally you'd write a class in cases where you want more than one of something. I'm not sure that's the case here. What exactly do you mean by a class operating on an input record? Or, to go at it a different way, what exactly is the flow of events that you want to handle? It may be that you could use a class called InputHandler (or whatever), and you'd do something like: ih = InputHandler.new(filename) fields = ih.parse_into_fields A class is a generalization. So if what you're doing isn't general, you may not need or want to model it in classes. If you're writing a script to parse one particular file, there's quite likely no point writing a generalized handler class. I feel like I'm failing to get beyond the rather unhelpful "it depends" level, but maybe prompt me with some further specifics.... David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!