Currently I'm developing a Watcher
class in Ruby, which, among other things, is able to find the period duration of a toggling signal. This is in general rather simple, but a problem I'm facing is that apparently Ruby passes all parameters by value.
While researching online I found many different discussions about what "pass by value" and "pass by reference" actually is, but no actual "how to". Coming from a C/C++ background, for me this is an essential part of a programming/scripting language.
The relevant parts of the class that I wrote are shown below. The method Watcher::watchToggling() is the one I'm having trouble with. As a parameter, aVariable
should be the reference to the value I'm measuring the period duration for.
class Watcher
@done
@timePeriod
def reset()
@done = false;
@timePeriod = nil;
end
def watchToggling(aVariable, aTimeout=nil)
oldState = aVariable;
oldTime = 0;
newTime = 0;
timeout(aTimeout)do
while(!@done)
newState = aVariable;
if(newState != oldState)
if(newState == 1)
# rising edge
if(oldTime != 0)
# there was already a rising edge before,
# so we can calculate the period
newTime = Time.now();
@timePeriod = newTime - oldTime;
@done = true;
else
# if there was no previous rising edge,
# set the time of this one
oldTime = Time.now();
end
end
oldState = newState;
end
end
end
puts("Watcher: done.")
end
def isDone()
return @done;
end
def getTimePeriod()
return @timePeriod;
end
end
Is there any way at all in ruby to pass by reference? And if not, are there alternatives that would solve this problem?