Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Array comparison.. why does this work.
Jean Nibee
3/1/2007 2:46:00 AM
How does Ruby do Array comparision?
[code]
old_inventory = [ 'a', 'b', 'd' ]
new_inventory = [ 'a','e', 'c' ]
puts "The following files have been added: "
puts new_inventory - old_inventory
puts ""
puts "The following files have been deleted: "
puts old_inventory - new_inventory
[/code]
Notice the output is correct so it's not a case of element 0's being
compared in each array.
So...
Does Ruby sort arrays in memory first? or is a scan done 1 element at a
time?
Thanks.
--
Posted via
http://www.ruby-...
.
2 Answers
dblack
3/1/2007 2:57:00 AM
0
Jean Nibee
3/1/2007 4:21:00 AM
0
>
> You're not doing comparison, though; you're doing subtraction.
> Subtraction doesn't depend on order. You can think of it as
> "without":
>
> [1,2,3] - [1] => [2,3] # [1,2,3] without 1
> [1,2,3,1] - [1] => [2,3] # [1,2,3,1] without 1
> [1,2,3] - [2] => [1,3] # [1,2,3] without 2
>
> etc.
>
>
> David
Sorry, when I mentioned comparison I meant "Under the hood". (I realise
'-' is subtractions ), but the interpreter needs to perform comparisoin
to see if an element exists somewhere.
I was curious if it just interates through one array looking for that
element in the second, or does it (internally) sort the array, iterate
each array performing comparision and if a > b skipping the equality
determination to save CPU cycles and doing a 'next'.
I guess what I"m saying (Makes more sense when you see pseudo code) is
how does it work in the interpreter.
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Array comparison.. why does this work.
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password