If I have two newly created unsaved objects, is there anyway to tell that they are not same object? Since the id gets assigned only when the object is persisted, and == is overridden, I''m not sure of how I can do this. Thanks David
On Jun 2, 2005, at 6:11 PM, David Corbin wrote:> If I have two newly created unsaved objects, is there anyway to tell > that they > are not same object? Since the id gets assigned only when the object > is > persisted, and == is overridden, I''m not sure of how I can do this.It looks like you can do: ar1.equal? ar2
On Thursday 02 June 2005 09:22 pm, Zach Thompson wrote:> It looks like you can do: > > ar1.equal? ar2Works great. Thanks. I always have trouble keeping all of Ruby''s equality methods straight.
On 03/06/2005, at 11:11 AM, David Corbin wrote:> If I have two newly created unsaved objects, is there anyway to tell > that they > are not same object? Since the id gets assigned only when the object > is > persisted, and == is overridden, I''m not sure of how I can do this.According to the AR::Base docs you should be able to use the == operator: "Returns true if the comparison_object is the same object, or is of the same type and has the same id." - tim lucas
On Friday 03 June 2005 11:24 pm, Tim Lucas wrote:> On 03/06/2005, at 11:11 AM, David Corbin wrote: > > If I have two newly created unsaved objects, is there anyway to tell > > that they > > are not same object? Since the id gets assigned only when the object > > is > > persisted, and == is overridden, I''m not sure of how I can do this. > > According to the AR::Base docs you should be able to use the => operator: > "Returns true if the comparison_object is the same object, or is of the > same type and has the same id."Right, but if the object hasn''t been saved, then the id is always 0 (or maybe it''s nil). David
On 05/06/2005, at 5:35 AM, David Corbin wrote:> On Friday 03 June 2005 11:24 pm, Tim Lucas wrote: >> On 03/06/2005, at 11:11 AM, David Corbin wrote: >>> If I have two newly created unsaved objects, is there anyway to tell >>> that they >>> are not same object? Since the id gets assigned only when the object >>> is >>> persisted, and == is overridden, I''m not sure of how I can do this. >> >> According to the AR::Base docs you should be able to use the =>> operator: >> "Returns true if the comparison_object is the same object, or is of >> the >> same type and has the same id." > > Right, but if the object hasn''t been saved, then the id is always 0 > (or maybe > it''s nil).Sorry, I must have misunderstood. You asked " is there anyway to tell that they are not same object". According to the docs, == "Returns true if the comparison_object is the same object", answering your question exactly. "The same object" in this case means the same object instance, i.e.: person1 = Person.new person2 = person1 a == b > True Maybe you meant to ask whether the objects were equivalent? (i.e. their properties contain equivalent data) - tim
> Sorry, I must have misunderstood.> You asked " is there anyway to tell > that they are not same object". According to the docs, == "Returns true > if the comparison_object is the same object", answering your question > exactly. > > "The same object" in this case means the same object instance, i.e.: > person1 = Person.new > person2 = person1 > a == b > > > Trueperson3 = Person.new person1 == person3 => true person1.equal? person3 => false person1.equal? person2 => true (I think) person1.save; person2.save person1 == person3 => false> > Maybe you meant to ask whether the objects were equivalent? (i.e. their > properties contain equivalent data) >No, I was looking for something like line# 3 above. David