From: Lennon Day-Reynolds Date: 2004-08-14T05:58:05+09:00 Subject: Re: Question: A method for summing several variables Harry, In that case, most of the nicer ways to do what you're looking for in Ruby are going to unavailable -- the whole reason that container types like arrays and hashes are in the core language is to make tasks like this easier. The best recommandation I'd have for you would be to define the sets of fields you're going to be working with as arrays, which will still give you some advantages. You'll have to manually list the fields you want to work with, but you can quickly perform operations on those entire lists, at least. For example, instead of writing 'box1_X = (box1_2 + box1_4 + ...)', do something like the following: current_boxset = [box1_2, box1_4, <...>] sum_of_boxset = current_boxset.inject(0.0) {|sum, current| sum += current.to_f} box1_X = sum_of_boxset (etc.) This will be especially useful if you define the sets of fields you're working with in some central place, and can maintain those datasets seperately from the algorithmic code that operates on them; it would even free you up to use more structured types later, so long as the algorithms eventually got the array of strings they expected. I'm not sure how often you need operations like 'sum' that operate over a list of values, but you can also add a convenience method to the Array type to clean up the uses of the code: class Array def sum(initial=0.0) self.inject(initial) {|sum, current| sum += current} end end ...and use it like this: my_array = [ '1.2', '3', '4.5' ] sum = my_array.map { |val| val.to_f}.sum Lennon rcoder.net P.S.: If you don't mind me asking, why the hard requirement that you stick with the unstructured variable types? Just stuck with the legacy code, or is there some sinister force at work?