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
···
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?
The key point is that the setVar method is supposed to do a couple
more things than just set the value of the variable. I removed them
from the example just for the sake of simplicity. Something like:
class MyClass
def setVar(var,varValue)
var=varValue #do something else...
end
Beeing able to "send" the variable to setVar and have its value
changed would save me many lines of code...
···
On 23 nov, 20:00, Morton Goldberg <m_goldb...@ameritech.net> wrote:
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.
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
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
The key point is that the setVar method is supposed to do a couple
more things than just set the value of the variable. I removed them
from the example just for the sake of simplicity.
That was a bad idea, since it made it impossible for anyone to understand your real problem and give you the help you were looking for.
Something like:
class MyClass
def setVar(var,varValue)
var=varValue #do something else...
end
The most stylish, Ruby way to do what you are trying to do is as follows:
class MyClass
def initialize @myVar1 = 0 # direct value manipulation
self.myVar1 = 1 # sent through your wrapper
puts @myVar1
end
def myVar1=(value) @myVar1 = value
# process extra steps here
end
end
(Ehm, I should admit, there may be a better way to actually get the
accessor method to fire inside of initialize than self.myVar1, but
that's what I found to work.)
···
>> 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?
I would still have to writer a myVar=(value) method for every single
variable. That's what I'm trying to get rid of.
···
On Nov 25, 11:50 am, Matt Todd <chiol...@gmail.com> wrote:
> >> On Nov 23, 2007, at 4:20 PM,Filipewrote:
> >>> 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?
The most stylish, Ruby way to do what you are trying to do is as follows:
class MyClass
def initialize @myVar1 = 0 # direct value manipulation
self.myVar1 = 1 # sent through your wrapper
puts @myVar1
end
def myVar1=(value) @myVar1 = value
# process extra steps here
end
end
(Ehm, I should admit, there may be a better way to actually get the
accessor method to fire inside of initialize than self.myVar1, but
that's what I found to work.)