From: ptkwt@... (Phil Tomson) Date: 2004-08-17T04:51:01+09:00 Subject: Re: A method for summing several variables In article <019801c483b7$4b338d20$1000000a@corp.stf.com>, Harry Truax wrote: >Thanks Austin, > >But unfortunately $SAFE needs to be set to 3 which will preclude the use of >eval( ). > >Harry > >----- Original Message ----- >From: "Austin Ziegler" >To: "ruby-talk ML" >Sent: Monday, August 16, 2004 1:13 PM >Subject: Re: A method for summing several variables > > >> On Tue, 17 Aug 2004 01:48:09 +0900, Harry Truax wrote: >> > Hi everyone, >> > >> > Just wanted to say thank you for all of your suggestions and coding >examples. I will be studying your ideas closely to see if I can implement >them in our product. I really >> > do not have the leeway to alter our product's code structure, I have to >live with this fact (argh!!). >> > >> > Our field variables (the ones we do arithematic on) are set up like: >> > >> > box1_1 = '' >> > box1_2 = '' >> > box1_3 = '' >> > box1_4 = '' >> > box1_5 = '' >> > box1_6 = '' >> > box1_7 = '' >> > box1_8 = '' >> > box1_9 = '' >> > box1_10 = '' >> > box1_11 = '' >> > etc. >> > >> > The actual amount and thus naming of these variables vary from >application to application. Some of the summing operations are quite lengthy >as in: >> > >> > box5_189 = $CUR % (box5_5.to_f + box5_10.to_f + box5_15.to_f + >box5_20.to_f + box5_25.to_f + box5_30.to_f + box5_35.to_f + box5_40.to_f + >box5_45.to_f + box5_50.to_f + box5_55.to_f + box5_60.to_f + box5_65.to_f + >box5_70.to_f + box5_75.to_f + box5_80.to_f + box5_85.to_f + box5_90.to_f + >box5_95.to_f + box5_100.to_f + box5_105.to_f + box5_110.to_f + box5_115.to_f >+ box5_120.to_f + box5_125.to_f + box5_130.to_f + box5_135.to_f + >box5_140.to_f + box5_145.to_f + box5_150.to_f + box5_155.to_f + >box5_160.to_f + box5_165.to_f + box5_170.to_f + box5_175.to_f + >box5_180.to_f + box5_185.to_f + box5_187.to_f) >> > >> > I will try to find a shortcut method to handle this based on your >suggestions, but if I have to manually type in each box name in setting up >the implementation, I may >> > as well just type in the lengthy sum operation as shown above. >> >> I didn't participate in the initial round, but why not do a generator >> and then eval your generated string in the current binding? >> >> s = (5..185).to_a.select { |e| e % 5 == 0 }.map { |e| "box5_#{e}.to_f" } >> s << "box5_187.to_f" >> box5_189 = $CUR % [eval("(#{s.join(' + ')})", binding)] >> >> This will work so long as there's some kind of pattern you can deal >> with (I don't get the box5_187 in your example, but I added it as an >> exception). >> >> I tested in irb with: >> >> v = (5..187).to_a >> v = v.select { |e| (e % 5 == 0) or (e == 187) }.map { |e| "box5_#{e}" } >> v << "box5_187" >> b = binding >> v.each_with_index { |e, i| eval("#{e} = 1.25", b) } >> puts box5_10 # => 1.25 >> $CUR = "%020.10f" >> >> # the above set the test data; the below is the implementation >> s = (5..187).to_a.select { |e| (e % 5 == 0) or (e == 187) } >> s.map! { |e| "box5_#{e}.to_f" } >> box5_189 = $CUR % [eval("(#{s.join(' + ')})", binding) >> puts box5_189 # => 000000047.5000000000 >> puts v.size * 1.25 # => 47.5 >> >> -austin >> -- >> Austin Ziegler * halostatue@gmail.com >> * Alternate: austin@halostatue.ca Here's a crazy idea: You can't do an 'eval' with $SAFE set to 3, but you could instead of generating Ruby code, you could generate C code and use something like RubyInline or Cgenerator (see: http://redshift.sourceforge.net/cgen/ ). Either one of those can take C code and generate a shared lib (dll) that can then be called from Ruby. The idea is generally the same as Austin's. The only drawback is if you're on a system that doesn't have a C compiler (*nix's generally do have a C compiler available, Windows generally doesn't). Phil