Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > array equality question

Reply
Thread Tools

array equality question

 
 
Chris McMahon
Guest
Posts: n/a
 
      01-22-2007

This took me by surprise:

irb(main):001:0> a1 = ['x','y','z']
=> ["x", "y", "z"]
irb(main):002:0> a2 = ['y','z','x']
=> ["y", "z", "x"]

irb(main):008:0> puts "yo" if a1 == a2
=> nil
irb(main):009:0> puts "yo" if a1 != a2
yo
=> nil

I assumed order of elements would not be considered for equality. Is
there any particular reason for this behavior?

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-22-2007
On 22.01.2007 23:27, Chris McMahon wrote:
> This took me by surprise:
>
> irb(main):001:0> a1 = ['x','y','z']
> => ["x", "y", "z"]
> irb(main):002:0> a2 = ['y','z','x']
> => ["y", "z", "x"]
>
> irb(main):008:0> puts "yo" if a1 == a2
> => nil
> irb(main):009:0> puts "yo" if a1 != a2
> yo
> => nil
>
> I assumed order of elements would not be considered for equality. Is
> there any particular reason for this behavior?


Why should order not matter? An array is an *ordered* list of elements
as indexing by position indicates. a1[0] == a2[0] will return false,
why then should a1 == a2 return true?

If you to ignore order you either have to compare sorted copies or
resort to Set.

Kind regards

robert
 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      01-22-2007
Chris McMahon wrote:
> This took me by surprise:
>
> irb(main):001:0> a1 = ['x','y','z']
> => ["x", "y", "z"]
> irb(main):002:0> a2 = ['y','z','x']
> => ["y", "z", "x"]
>
> irb(main):008:0> puts "yo" if a1 == a2
> => nil
> irb(main):009:0> puts "yo" if a1 != a2
> yo
> => nil
>
> I assumed order of elements would not be considered for equality. Is
> there any particular reason for this behavior?


The reason for this behavior is that the creator of Ruby
and most of its users aren't devoid of knowledge of
programming. An array is not a set. An array is not a
hash (a.k.a. associative array). So of course order matters.
Does the order in which you read the chapters of a novel
matter? If order doesn't matter, then why is there a 'sort'
method that changes the order of an array?

 
Reply With Quote
 
gwtmp01@mac.com
Guest
Posts: n/a
 
      01-22-2007

On Jan 22, 2007, at 5:30 PM, Chris McMahon wrote:
> I assumed order of elements would not be considered for equality. Is
> there any particular reason for this behavior?


The concept of order is an integral part of what it means to be an
Array.
If you aren't interested in order, then a Set is probably a better
structure.

require 'set'

a = [1,2,3]
b = a.reverse
p a == b # false
p Set.new(a) == Set.new(b) # true

Gary Wright




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Equality for Different Array Objects Shao Miller C Programming 11 02-06-2012 07:25 AM
Array#=== as set equality Intransition Ruby 6 09-01-2010 04:15 PM
Test equality of each element in an array 12 34 Ruby 15 07-03-2007 01:48 AM
Question on vector assignment and object equality determnation Alfonzo Morra C++ 1 07-12-2005 10:23 PM
Question on vector assignment and object equality determination Alfonzo Morra C++ 9 07-11-2005 03:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57