Last active
August 29, 2015 14:24
-
-
Save kristm/3d26ba1a31860119e92b to your computer and use it in GitHub Desktop.
group in pairs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#get array of cities by | |
#Schools.where('XX').map(&:city).uniq.sort { |a,b| a.downcase <=> b.downcase } | |
def group_in_pairs (arr) | |
ra = [] | |
skip = -1 | |
arr.each_with_index do |a,i| | |
return ra if arr[i+1].nil? | |
next if skip > i | |
if (a.downcase == arr[i+1].downcase) | |
ma = [] | |
ni = i+1 | |
ma.push a | |
while (a.downcase == arr[ni].try(:downcase)) | |
ma.push arr[ni] | |
ni = ni + 1 | |
end | |
ra << ma | |
skip = ni | |
else | |
ra << [a.titleize, a] unless a.titleize == a | |
end | |
end | |
ra | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment