Stefan Lang
8/8/2008 3:49:00 PM
2008/8/8 tobyclemson@gmail.com <tobyclemson@gmail.com>:
> Sorry I don't quite understand the problem - I can see that it
> probably is one but I think it's a matter of terminology. What do you
> mean when you say destructively modified? I am modifying the value of
> the timestamp in place? So that any reference to that timestamp will
> be modified too? Should I be doing a duplication on the string that is
> used to key the buffer in the buffers hash? I didn't think that the
> actual object was passed in when an argument is supplied, I thought a
> copy of it was passed in..
>
> How would I make Timestamp#advance nondestructive?
> If it is easier than pasting here I can give you commmit priveleges on
> that repository?
Arguments are passed by reference. Not a reference to the variable,
but a reference to the object. That's how most OO languages work.
Regarding your program: Add an accessor for the :time to the
Timestamp class, then change the advance definition
to this:
def advance
ts = self.dup
ts.time += 60
ts
end
Instead of modifying the instance, we create a new one with the
desired change.
Now in the main loop in dedupe change this line:
timestamp.advance
to
timestamp = timestamp.advance
This way ChecksumBuffer#add will actually get a different
timestamp object on each call.
Since you also use Enumerable#min on an array of Timestamp
objects, you need to add Timestamp#<=>:
def <=>(other)
self.time <=> other.time
end
That should do it.
> Thanks very much for your help,
You're welcome!
Stefan