From: Harry Truax Date: 2004-08-14T03:46:10+09:00 Subject: Question: A method for summing several variables ------=_NextPart_000_00A3_01C48144.362462D0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi everyone, I am new to Ruby and have a basic question.=20 My company uses Ruby in implementing calculations for various forms, = like a federal 1040 form for example. Each calculatable field on the = form is represented as 'box1_1' - this means the first box on page 1 of = the form, 'box1_2' is the second box (or field) on page 1, etc..=20 We do several calculations such as adding together several boxes as in = this example:=20 box1_37 =3D $CUR (box1_2.to_f + box1_4.to_f + box1_6.to_f +..etc)=20 Where $CUR =3D '%0.2f' This can be tedious, as we have some calculations that add up as many as = one hundred boxes. We set $SAFE to 3 in our application - it needs to be = this value.=20 We initially set all boxes to '' as in this example:=20 box1_1 =3D '' box1_2 =3D '' . . box12_1 =3D '' box12_2 =3D '' etc. . .=20 How can I add up a range of boxes without having to specifiy each box by = name? Maybe a nice short method I could call, giving it the starting box = and the step value (sometimes the box numbers differ by a value other = than 1, like my example above where they differ by 2) and the number of = boxes to add.=20 Below is a method I tried, but 'box' was not being properly handled - = box could be say, 'box1_10', but I want the value that 'box1_10' points = to, not the .to_f value of the string 'box1_10'.=20 def add_boxes(box, step, count) #Init local variables. sum =3D 0 box_num =3D box[box.rindex('_') + 1,(box.length - 1) - = box.rindex('_')].to_i box_prefix =3D box[0, box.rindex('_') + 1] =20 0.upto(count) { sum +=3D box.to_f box_num +=3D step box =3D box_prefix + box_num.to_s } end =20 Thanks a lot for your help, I have been out of the programming scene for = a few years and any help is greatly appreciated.=20 Regards,=20 Harry Truax=20 ------=_NextPart_000_00A3_01C48144.362462D0--