Module: CgpaCalculator
- Defined in:
- lib/cgpa_calculator.rb
Overview
CGPA Calculator - A lightweight Ruby library for calculating GPA, CGPA, and SGPA. Supports 4.0 and 5.0 grading scales.
Homepage: https://gpacalc.app
Constant Summary collapse
- VERSION =
"1.0.1"- GRADE_POINTS_4 =
{ "A+" => 4.0, "A" => 4.0, "A-" => 3.7, "B+" => 3.3, "B" => 3.0, "B-" => 2.7, "C+" => 2.3, "C" => 2.0, "C-" => 1.7, "D+" => 1.3, "D" => 1.0, "D-" => 0.7, "F" => 0.0 }.freeze
- GRADE_POINTS_5 =
{ "A" => 5.0, "B" => 4.0, "C" => 3.0, "D" => 2.0, "E" => 1.0, "F" => 0.0 }.freeze
Class Method Summary collapse
-
.calculate_cgpa(semesters) ⇒ Float
Calculate cumulative GPA across multiple semesters.
-
.calculate_gpa(courses, scale: 4.0) ⇒ Float
Calculate GPA for a list of courses.
-
.gpa_to_letter(gpa) ⇒ String
Convert a GPA to a letter grade.
-
.percentage_to_gpa(percentage) ⇒ Float
Convert a percentage score to GPA on a 4.0 scale.
Class Method Details
.calculate_cgpa(semesters) ⇒ Float
Calculate cumulative GPA across multiple semesters
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cgpa_calculator.rb', line 51 def calculate_cgpa(semesters) raise ArgumentError, "semesters must be a non-empty array" if semesters.nil? || semesters.empty? total_points = 0.0 total_credits = 0.0 semesters.each do |sem| gpa = sem[:gpa].to_f credits = sem[:credits].to_f total_points += gpa * credits total_credits += credits end (total_points / total_credits).round(2) end |
.calculate_gpa(courses, scale: 4.0) ⇒ Float
Calculate GPA for a list of courses
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cgpa_calculator.rb', line 28 def calculate_gpa(courses, scale: 4.0) raise ArgumentError, "courses must be a non-empty array" if courses.nil? || courses.empty? grade_map = scale == 5.0 ? GRADE_POINTS_5 : GRADE_POINTS_4 total_points = 0.0 total_credits = 0.0 courses.each do |course| grade = course[:grade].to_s.upcase.strip credits = course[:credits].to_f raise ArgumentError, "Unknown grade: #{grade}" unless grade_map.key?(grade) raise ArgumentError, "Credits must be positive" unless credits > 0 total_points += grade_map[grade] * credits total_credits += credits end (total_points / total_credits).round(2) end |
.gpa_to_letter(gpa) ⇒ String
Convert a GPA to a letter grade
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cgpa_calculator.rb', line 92 def gpa_to_letter(gpa) case gpa when 3.85..4.0 then "A" when 3.5...3.85 then "A-" when 3.15...3.5 then "B+" when 2.85...3.15 then "B" when 2.5...2.85 then "B-" when 2.15...2.5 then "C+" when 1.85...2.15 then "C" when 1.5...1.85 then "C-" when 1.15...1.5 then "D+" when 0.85...1.15 then "D" when 0.5...0.85 then "D-" else "F" end end |
.percentage_to_gpa(percentage) ⇒ Float
Convert a percentage score to GPA on a 4.0 scale
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/cgpa_calculator.rb', line 70 def percentage_to_gpa(percentage) raise ArgumentError, "Percentage must be between 0 and 100" unless percentage.between?(0, 100) case percentage when 93..100 then 4.0 when 90..92 then 3.7 when 87..89 then 3.3 when 83..86 then 3.0 when 80..82 then 2.7 when 77..79 then 2.3 when 73..76 then 2.0 when 70..72 then 1.7 when 67..69 then 1.3 when 63..66 then 1.0 when 60..62 then 0.7 else 0.0 end end |