From: Robert Klemme Date: 2010-08-22T18:15:13+09:00 Subject: Re: A small problem for arrays On 21.08.2010 18:27, Jean-Julien Fleck wrote: > 2010/8/21 Unc88 Unc88: >> Can do the subtraction of arrays without using the operator "-"? >> Is there a standard method in the class Array? > > "-" *is* a standard method of class Array. The apparent "operator" > behavior is just syntaxic sugar added on it: > >>> a1 = (1..10).to_a > => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> a2 = (5..7).to_a > => [5, 6, 7] >>> a1.-(a2) > => [1, 2, 3, 4, 8, 9, 10] > > Last line call "-" method of object "a1" with object "a2" as an argument. > > ~> ri Array.- > > ---------------------------------------------------------------- Array#- > array - other_array -> an_array > ------------------------------------------------------------------------ > Array Difference---Returns a new array that is a copy of the > original array, removing any items that also appear in other_array. > (If you need set-like behavior, see the library class Set.) > > [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ] > > Cheers, > Just adding to that: if Arrays are large and / or there are frequent set operations needed then using class Set might yield better performance. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/