From: Michael Morin Date: 2008-11-08T04:55:53+09:00 Subject: Re: array.equal? Nick Bhanji wrote: > Hello All, > > I am not sure where to ask this, but someone after reading this can tell > me more. > > I am trying to right a functions where I am comparing 2 arrays. Here is > an example. > > a = [1,1] > b = [1,1] > > in the irb, I executed a.equal? b and the result came as false. > > here is the output: > irb(main):031:0> a =[1,1] > => [1, 1] > irb(main):032:0> b =[1,1] > => [1, 1] > irb(main):033:0> a.equal? b > => false > irb(main):034:0> > > however when I execute a.eql? b the result is true. > > My questions: > 1. why is equal? listed in the methods for the array > 2. what is the definition of equal? in the case of array? > 3. why is there another function eql? performing the actual equal test > > I am using ruby version 1.8.6 for the windows > > Thank you in advance. > > Nick.B The equal? method comes from the Object class. The equal? method will return true if and only if the two objects are the same object. The two lists are two independently created lists, so this will be false. The eql? method is an alias for this method. If you want to see if two arrays contain the same objects, use the == operator. a = [1,1] b = [1,1] print "yes" if a == b