From: Robert Klemme Date: 2009-12-13T21:02:34+09:00 Subject: Re: Code block for element comparison in an array? On 13.12.2009 12:27, Derek Cannon wrote: > I'm new to Ruby and I can't think of how to do this! I would like each > element in an array to compare itself with all its proceeding elements. > > For example: > > In an array of 10 elements, > a[0] will be compared to a[1] ... a[9] > a[1] will be compared to a[2] ... a[9] > .. > a[9] will not be compared, as there is nothing larger than it. > > For those who don't understand, I think the Java/C++ equivalent would > be: > > for(int i=0; i< array.length; i++) { > for(int j=i+1; j< array.length; j++) { > // Compare array[i] with array[j] > } > } Just translate this directly to Ruby and you have your solution. Hint: you can use Fixnum#upto. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/