Makasih atas ilmunya, makanya untuk menyatakan kesamaan, perlu overwrite
definisi kesamaan (equality) ini di class-nya, dalam hal ini model-nya. Set
hanya atribut2nya saja yang sama, namun (mungkin, CMIIW) referensi-nya tidak
perlu sama. Saya copas dari jawaban milis tetangga agar thread ini menjadi
bermanfaat:
It all depends what we mean by *equal* (in human languages too). Quick
(simplified) summary of equality in Ruby:
a) first, assert_equal is the same to the == operator
b) now, the == operator is inherited from Object, and it tests if two *object
references* are identical.
c) however, the == operator is redefined by the usual classes String, Array,
Hash, etc to mean *objects have identical content*.
The 3 facts above indicate why the test behaved as you saw; the 2 Ruby
Recipe instances were tested for object identity, and the test failed,
because they were different instances. There is no error.
However, if you wished the test to behave as you expected, simply redefine
the == method in Recipe
class Recipe
def ==(other)
# compare properties of self against the ones of other
# self.x == other.x && self.y == other.y &&....
end
end
Demikian, mudah2an bermanfaat juga buat yang lain.
Thanks semuanya,
Dida
2008/9/17 irfani YangBaikHati <[EMAIL PROTECTED]>
> Pak adinda, di semua Bahasa OOP, kalau ingin membandingan equality
> object itu adalah dgn object id / key / hashcode atau digabung jg dgn
> to_string utk membandingkan dua object, jadi dua object yg dimata
> manusia memiliki value (dari suatu properti obj tsb, misal : titi punya
> koordinat x dan y) sama belum tentu sama jika dibandingkan equality nya,
> jika instance nya beda ya beda object id nya, mereference ke tempat yg
> beda.
>
> kalo object2 tipe primitive, String, Fixnum, Boolean, dll di kecualikan,
> mungkin ini udah langsung implement method eql? yg langsung bandingin
> value nya dari Kernel Object (CMIIW)
>
>
[Non-text portions of this message have been removed]