From: "Jesús Gabriel y Galán" Date: 2010-08-14T17:38:42+09:00 Subject: Re: Does Ruby have reference parameter? On Sat, Aug 14, 2010 at 9:17 AM, Chan Nguyen wrote: > Hi everyone, > In C++, in order to modify a variable being passed into a function we do > [code] > void func( int& x ) > { >    x = 10; > } > [/code] > > I wonder is there a way to do something like this in Ruby or a similar > idea? If there's no reference type then how would we be able to modify a > varible? In Ruby a variable is a reference to an object, and when you call a method passing the variable as a parameter, what the method gets it's a copy of the reference, so you can't modify a variable inside a method. That means, changing which object the variable refers to: def change(arg) arg = "another thing" end a = "a string" change(a) a would still be referencing "a string", because arg inside the method is a copy of the reference. The usual way is to have the method return the new value and assign it: def change(arg) "a better #{arg}" end a = "a string" a = change(a) or to use mutable methods: def change(arg) arg.replace("a better #{arg}") end a = "a string" change(a) This works because you are not changing which object is referenced by a, but you change the internal state of that object. Jesus.