From: Chris Thiel Date: 2007-07-26T02:59:38+09:00 Subject: Re: Question - Passing parameters by reference ------=_Part_156267_14822382.1185386382236 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 7/25/07, Todd Benson wrote: > > On 7/25/07, caof2005 wrote: > > Hello Folks > > > > My question is the following: > > > > How can I pass a reference to a method as an argument, so after > > finishing the execution of the method the argument gets updated with a > > new value. ( known as "pass by reference" in other languages ). > > > > example: > > .... > > def changeValue( val, cad ) > > temp = 0; > > val = (val * 10) / cad.to_i > > temp = cad.to_i + 3.to_i > > cad = temp.to_s > > end > > .... > > .... > > val = 35; > > cad = "7" > > > > puts "val before calling changeValue:= " + val.to_s > > puts "cad before calling changeValue:=" + cad > > > > changeValue( val, cad ) > > > > puts "val before calling changeValue:= " + val.to_s #--- I pretend > > to print '50' > > puts "cad before calling changeValue:=" + cad #--- I pretend > > to print '10' > > > > Regards > > Carlos > > Assignment inside method scope is allowed, but creates a different > object. What you get back from a method is what the method returns. > So you could mimic your desired behavior with... > > def f(a, b) > return a*10.0/b.to_i, (b.to_i+3).to_s > end > > x, y = 35, "7" > x, y = f( x, y ) #this will make x equal to 50 and y equal to the string > "10" > > Does that make sense? > > Todd > > What if the method is returning something else already and you don't want to have to return the changed value? How do you pass in a variable to a method and have the change stick when it exists the method in ruby? Chris ------=_Part_156267_14822382.1185386382236--