Skip to content

Instantly share code, notes, and snippets.

@mfifth
Last active March 2, 2019 17:21
Show Gist options
  • Save mfifth/46fb313965ab679409dedf1765232e52 to your computer and use it in GitHub Desktop.
Save mfifth/46fb313965ab679409dedf1765232e52 to your computer and use it in GitHub Desktop.
class Grade
include Comparable
attr_reader :grade
def initialize(string)
@grade = string.downcase
end
def ==(other_grade)
@grade == other_grade.grade
end
def <=>(other_grade)
letter = other_grade.grade.sub(/[-+]/, '')
grade = @grade.sub(/[-+]/, '')
if grade == letter
return 1 if other_grade.grade.include?("+")
return -1 if other_grade.grade.include?('-')
0
elsif grade < letter
-1
elsif grade > letter
1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment