From: Morton Goldberg Date: 2007-11-24T07:00:48+09:00 Subject: Re: Class variable as parameter On Nov 23, 2007, at 4:20 PM, Filipe wrote: > I have the following situation: > > class MyClass > def setVar(var,varValue) > var=varValue > end > > def initialize > @myVar=0 > setVar(@myVar,1) > puts(@myVar.to_s) > end > end > > test = MyClass.new > > Unfortunately, this piece of code is returning 0 instead of the 1 I > would expect. Is there anything I could do? Your call to setVar can not change the value of an variable. Only values of variables are passed in Ruby, not the variables themselves. def setVar(var,varValue) var=varValue # <-- this is the same as: return varValue end def initialize @myVar=0 setVar(@myVar,1) # <-- at this point this is the same as: setVar(0,1) puts(@myVar.to_s) end Ruby has a built-in method, Object#instance_variable_set that you can use for situations where you need to assign by name, so you don't need to define it for any derived class. Also, Ruby can define accessor methods for you: class MyClass attr_accessor :myVar def initialize @myVar=0 self.myVar = 1 puts myVar # <-- to_s not needed; puts applies to_s to objects end end test = MyClass.new This will output 1 and may be what you are looking for. Regards, Morton