From: Glen Holcomb Date: 2007-07-26T04:00:26+09:00 Subject: Re: Question - Passing parameters by reference ------=_Part_156216_3010638.1185390024584 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 7/25/07, Chris Thiel wrote: > > On 7/25/07, dblack@wobblini.net wrote: > > > > Hi -- > > > > On Thu, 26 Jul 2007, Chris Thiel wrote: > > > > > 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? > > > > You can change an object: > > > > def change_me(str) > > str.upcase! > > end > > > > s = "abc" > > change_me(str) > > puts s # ABC > > > > But you can't change the bindings. In the example, I've got s and str > > both containing references to the same string. But the names of the > > identifiers themselves, s and str, are solely the business of their > > local scopes, which are different as between the method body and the > > calling context. > > > > > > David > > > > -- > > * Books: > > RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) > > RUBY FOR RAILS (http://www.manning.com/black) > > * Ruby/Rails training > > & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) > > > > I'm talking in a case like this: > def add_one(number) > number+=1 > end > n = 1 > add_one(n) > > While the method returns 2, the value of n when the method is done is > still > one. > > Or with strings > def add_bar(st) > st + "bar" > end > > s = "foo" > add_bar(s) > > Same thing. The method returns foobar, but the value of s is unchaged. > Is there a way to change the values of these variables from within the > method and have the values remain changed when the method has exited? > > Chris > n = add_one(n) would fix your given case. I assume it is more complicated than this? -- "Hey brother christian with your high and mighty errand, Your actions speak so loud, I can't hear a word you're saying." -Greg Graffin (Bad Religion) ------=_Part_156216_3010638.1185390024584--