Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/bio/transmembrane.rb

Overview

Monkey-patch Array#pair in

Instance Method Summary collapse

Instance Method Details

#pairs(another_array = nil) ⇒ Object

Return an array of all pairs of elements from this array (each is an array). If another_array is not nil, then do pairwise between this array and that (but not within each)

NOT thread safe.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bio/transmembrane.rb', line 8

def pairs(another_array = nil)
  pairs = []
  
  if another_array #between this array and the next
   (0..length-1).each do |index1|
   (0..another_array.length-1).each do |index2|
        pairs.push [self[index1], another_array[index2]]
      end
    end
  else # within this array only
   (0..length-1).each do |index1|
      index2 = index1+1
      while index2 < length
        pairs.push [self[index1], self[index2]]
        index2 += 1
      end
    end
  end
  
  return pairs
end