From: Patrick Hurley Date: 2006-08-27T12:46:30+09:00 Subject: Re: Ruby == how does it works? On 8/26/06, Surendra Singhi wrote: > >> Does the Ruby '==' operator compares the 'object_ids' of the > >> element before > >> descending into comparing the elements? > > > > Depends on how the object receiving it implements it. > > > And how does hashes, and arrays implement it? ri Hash#== ---------------------------------------------------------------- Hash#== hsh == other_hash => true or false ------------------------------------------------------------------------ Equality---Two hashes are equal if they each contain the same number of keys and if each key-value pair is equal to (according to +Object#==+) the corresponding elements in the other hash. h1 = { "a" => 1, "c" => 2 } h2 = { 7 => 35, "c" => 2, "a" => 1 } h3 = { "a" => 1, "c" => 2, 7 => 35 } h4 = { "a" => 1, "d" => 2, "f" => 35 } h1 == h2 #=> false h2 == h3 #=> true h3 == h4 #=> false ri Arrray#== --------------------------------------------------------------- Array#== array == other_array -> bool ------------------------------------------------------------------------ Equality---Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object.==) the corresponding element in the other array. [ "a", "c" ] == [ "a", "c", 7 ] #=> false [ "a", "c", 7 ] == [ "a", "c", 7 ] #=> true [ "a", "c", 7 ] == [ "a", "d", "f" ] #=> false