From: Jason Roelofs Date: 2007-07-26T04:34:12+09:00 Subject: Re: Question - Passing parameters by reference ------=_Part_52299_27446858.1185392054954 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline You're pretty close, just some details as to the whyness: String are of course instances of the String class. This is simply an array of characters, of which you can modify freely, as you've discovered. Numbers, on the other hand are special case instances of FixNum, BigNumber, any subclass of Numeric. These have no underlying data representation, they *are* the number they represent. So if you tried to do the same to Fixnums like you can with String, you'd actually be trying to change 1 to equal 2, and you can imagine what kind of havok that would cause. So with Strings, you have an underlying character array you can play with, but with Numerics you have to change the number your variable is pointing to. Hopefully I made that understandable, though please correct me if something I said was wrong. Jason On 7/25/07, caof2005 wrote: > > Thanks for the response, although I was just wondering that using > Strings as a parameters in a method definition is a special case. > Let me explain.... > I observed that when you use a string as a parameter in a method > definition, it can be changed by applying several operations inside > the method, for instance you can change it, by adding letters to the > string, changing the content to uppercase etc. > So I figured out that when you use a string, what really happens > inside the method is that you don't create a new object, you just use > the reference. > In opposition as if you use other kind of parameters in a method > definition, let say for example an int variable. > If you try to change this int variable inside the method, it > simply doesn't work, what happens is that you get a "copy" of the > object that you're passing but after the method is finished, that > copy disappears and at the end the parameter that you passed was not > affected. > Am I correct with my conclusions? > > Regards > Carlos > > On Jul 25, 11:35 am, dbl...@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) > > > > ------=_Part_52299_27446858.1185392054954--