Skip to content

Instantly share code, notes, and snippets.

@mlevans
Created September 25, 2012 23:31
Show Gist options
  • Save mlevans/3785085 to your computer and use it in GitHub Desktop.
Save mlevans/3785085 to your computer and use it in GitHub Desktop.
class GradeLevel
attr_accessor :number
def initialize(num)
@number = num
end
def order_index
number =~ /K(\d)/ ? $1.to_i : (number.to_i + 2)
end
end
def short_string(collection)
# as_i = collection.all.map{|x| x.order_index}.sort
as_i = collection.map(&:order_index).sort
collection.sort_by(&:order_index).slice_before {|x| !as_i.include?(x.order_index-1)}.map {|arr| "#{arr.first.number}-#{arr.last.number}"}*", "
end
grades = ["K0", "K1", "K2", "4", "5", "6", ].map {|num| GradeLevel.new(num)}
short_string(grades)
@YenTheFirst
Copy link

def order_index(number)
  number =~ /K(\d)/ ? $1.to_i : (number.to_i + 2)
end

def short_string(collection)
  #in this case, we're calling order_index with an argument, not as a method on an object
  as_i = collection.map {|num| order_index(num)}.sort
  collection.sort_by {|x| order_index(x)}.slice_before {|x| !as_i.include?(order_index(x)-1)}.map {|arr| "#{arr.first}-#{arr.last}"}*", "
end

will also work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment