Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Generating Functions in Ruby Date: Tue, 24 May 2011 17:53:54 -0500 Organization: Service de news de lacave.net Lines: 91 Message-ID: References: <5e21af14-befb-406e-b1c3-6758fe1527ab@s14g2000vbi.googlegroups.com> <151562ef-aebd-4999-8b7b-816c21a7c6df@m40g2000vbt.googlegroups.com> <917a6bebd84192c90d0434fe9ec9e2a4@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1306277931 4073 65.111.164.187 (24 May 2011 22:58:51 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Tue, 24 May 2011 22:58:51 +0000 (UTC) In-Reply-To: <917a6bebd84192c90d0434fe9ec9e2a4@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 383719 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:5010 7stud -- wrote in post #1000754: > Andreas Lundgren wrote in post #1000535: >> First params does not contain a simple string; >> please note the escaped string characters in the code >> that generates params. Kind of strings within strings > > What I failed to recognize was that the line: > > params = #{params} > > was part of a multiline string. > > > >> "1) Strings are mutable in ruby, so get rid of all those +'s." - I'm >> not sure that I understand this but it sounds interesting, what does >> it mean? >> > > Every quoting mechanism creates a string and every + creates a new > combined string. So it's more efficient to use string interpolation: > > i = 2 > params = 'x1.to_s' > params = "#{params}, x#{i}.to_s" > p params > > --output:-- > "x1.to_s, x2.to_s" > > > In ruby not only can you push elements onto an array with the << method, > you can also use the << method to push a string onto another > string--which > alters the first string: > > > i = 2 > params = 'x1.to_s' > params = "#{params}, x#{i}.to_s" > > puts params > puts params.object_id > > params << ', x3.to_s' > > puts params > puts params.object_id > > --output:-- > x1.to_s, x2.to_s > 77684510 > x1.to_s, x2.to_s, x3.to_s > 77684510 So your loop here: params = 'x1.to_s'; for i in 2..no_of_params do arg_list = arg_list + ', x' + i.to_s; params = params + ' + \', \' + x' + i.to_s + '.to_s'; end params = params+';'; would simplify to: num_params = 4 params = 'x1.to_s' 2.upto(num_params).each do |i| params << ", x#{i}.to_s" end p params --output:-- "x1.to_s, x2.to_s, x3.to_s, x4.to_s" Also, compare that output to the output your loop produces: "x1.to_s + ', ' + x2.to_s + ', ' + x3.to_s + ', ' + x4.to_s" You would get many errors using that string as an argument list for a method. -- Posted via http://www.ruby-forum.com/.