From: Andreas Lundgren Date: 2011-05-24T22:50:27+09:00 Subject: Re: Generating Functions in Ruby > > First params typically assembles to this: > > x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s > > That does not look good.  If you have to encode numbers in variable > names, you probably rather want to use an Array for that or even > generate that directly: > > params = no_of_params.times.map {|i| "x#{i}"}.join(', ') your example generates code looks like this: params = x1, x2, x3, x4 But with that I get an error (eval):3:in `+': can't convert Fixnum into String (TypeError) If it should work, I recon that it has to look like this: params = "\"\#{" + no_of_params.times.map {|i| "x#{i}"}.join("}, \#{") + "}\"" becaus I need the generated code to look like this: params = "#{x1}, #{x2}, #{x3}, #{x4}" > handle.call(['MethodNameIn','Params'],[#{method},params]); Yes, this was a bug... > params_2 = "#{x1}, #{x2}, #{x3}, #{x4}" Yes, I could use this, but still I need to assembly this line since I dont know the number of input parameters at coding time. And I know that no input parameters contain any expressions that needs to be evaluated since it is an input from my generated function. Join and times.map was kind of cool, my prototype has now replaced the first loops with this: arg_list = no_of_params.times.map {|i| "x#{i}"}.join(", ") args = no_of_params.times.map {|i| "x#{i}.to_s"}.join("+','+") (Using #{} instead of .to_s is basically the same: args = "\"\#{" + no_of_params.times.map {|i| "x#{i}"}.join("}, \#{") + "}\"" BR, Andreas