cgpa_calculator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +62 -0
  4. data/lib/cgpa_calculator.rb +109 -0
  5. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 18965c9536ae0e7dce1d40c3947a80c7aecb895e07bea9fbe856409edc230f5f
4
+ data.tar.gz: 7cb91a77c5994a415f79ab31f6466c5927adb70dc3b425a1832e89be30a7d9fb
5
+ SHA512:
6
+ metadata.gz: 06062f1f180125176c11749f298d8d455965b53271a9cee926e1de7d1054d4c90cf981472117ac73bb34a65be13b507b75f043282d897fc69631995922e0f756
7
+ data.tar.gz: 0e861acf9f1b4a1104a2e1e003fec1cc4c71d35da4e7cece5eb858d931fdebc96ac96f7fb31bf8ffaa61942554eddb3f00b6a719bf1fdea60e306c75bc8f995f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 GPA Calculator Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # CGPA Calculator
2
+
3
+ A lightweight Ruby library for calculating GPA, CGPA, and SGPA. Supports 4.0 and 5.0 grading scales used by universities worldwide.
4
+
5
+ **Try the online tool: [gpacalc.app](https://gpacalc.app)**
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem install cgpa_calculator
11
+ ```
12
+
13
+ Or add to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'cgpa_calculator'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'cgpa_calculator'
23
+
24
+ # Calculate semester GPA
25
+ courses = [
26
+ { grade: "A", credits: 3 },
27
+ { grade: "B+", credits: 4 },
28
+ { grade: "A-", credits: 3 }
29
+ ]
30
+ gpa = CgpaCalculator.calculate_gpa(courses)
31
+ puts gpa # => 3.59
32
+
33
+ # Calculate cumulative GPA
34
+ semesters = [
35
+ { gpa: 3.5, credits: 15 },
36
+ { gpa: 3.8, credits: 16 }
37
+ ]
38
+ cgpa = CgpaCalculator.calculate_cgpa(semesters)
39
+ puts cgpa # => 3.66
40
+
41
+ # Convert percentage to GPA
42
+ gpa = CgpaCalculator.percentage_to_gpa(88)
43
+ puts gpa # => 3.3
44
+
45
+ # Convert GPA to letter grade
46
+ letter = CgpaCalculator.gpa_to_letter(3.5)
47
+ puts letter # => "A-"
48
+ ```
49
+
50
+ ## 5.0 Scale
51
+
52
+ ```ruby
53
+ courses = [
54
+ { grade: "A", credits: 3 },
55
+ { grade: "B", credits: 4 }
56
+ ]
57
+ gpa = CgpaCalculator.calculate_gpa(courses, scale: 5.0)
58
+ ```
59
+
60
+ ## License
61
+
62
+ MIT License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CGPA Calculator - A lightweight Ruby library for calculating GPA, CGPA, and SGPA.
4
+ # Supports 4.0 and 5.0 grading scales.
5
+ #
6
+ # Homepage: https://gpacalc.app
7
+ module CgpaCalculator
8
+ VERSION = "1.0.0"
9
+
10
+ GRADE_POINTS_4 = {
11
+ "A+" => 4.0, "A" => 4.0, "A-" => 3.7,
12
+ "B+" => 3.3, "B" => 3.0, "B-" => 2.7,
13
+ "C+" => 2.3, "C" => 2.0, "C-" => 1.7,
14
+ "D+" => 1.3, "D" => 1.0, "D-" => 0.7,
15
+ "F" => 0.0
16
+ }.freeze
17
+
18
+ GRADE_POINTS_5 = {
19
+ "A" => 5.0, "B" => 4.0, "C" => 3.0,
20
+ "D" => 2.0, "E" => 1.0, "F" => 0.0
21
+ }.freeze
22
+
23
+ class << self
24
+ # Calculate GPA for a list of courses
25
+ # @param courses [Array<Hash>] each with :grade (String) and :credits (Numeric)
26
+ # @param scale [Float] 4.0 or 5.0 (default: 4.0)
27
+ # @return [Float] the calculated GPA
28
+ def calculate_gpa(courses, scale: 4.0)
29
+ raise ArgumentError, "courses must be a non-empty array" if courses.nil? || courses.empty?
30
+
31
+ grade_map = scale == 5.0 ? GRADE_POINTS_5 : GRADE_POINTS_4
32
+ total_points = 0.0
33
+ total_credits = 0.0
34
+
35
+ courses.each do |course|
36
+ grade = course[:grade].to_s.upcase.strip
37
+ credits = course[:credits].to_f
38
+ raise ArgumentError, "Unknown grade: #{grade}" unless grade_map.key?(grade)
39
+ raise ArgumentError, "Credits must be positive" unless credits > 0
40
+
41
+ total_points += grade_map[grade] * credits
42
+ total_credits += credits
43
+ end
44
+
45
+ (total_points / total_credits).round(2)
46
+ end
47
+
48
+ # Calculate cumulative GPA across multiple semesters
49
+ # @param semesters [Array<Hash>] each with :gpa (Float) and :credits (Numeric)
50
+ # @return [Float] the cumulative GPA
51
+ def calculate_cgpa(semesters)
52
+ raise ArgumentError, "semesters must be a non-empty array" if semesters.nil? || semesters.empty?
53
+
54
+ total_points = 0.0
55
+ total_credits = 0.0
56
+
57
+ semesters.each do |sem|
58
+ gpa = sem[:gpa].to_f
59
+ credits = sem[:credits].to_f
60
+ total_points += gpa * credits
61
+ total_credits += credits
62
+ end
63
+
64
+ (total_points / total_credits).round(2)
65
+ end
66
+
67
+ # Convert a percentage score to GPA on a 4.0 scale
68
+ # @param percentage [Numeric] the percentage score (0-100)
69
+ # @return [Float] the equivalent GPA
70
+ def percentage_to_gpa(percentage)
71
+ raise ArgumentError, "Percentage must be between 0 and 100" unless percentage.between?(0, 100)
72
+
73
+ case percentage
74
+ when 93..100 then 4.0
75
+ when 90..92 then 3.7
76
+ when 87..89 then 3.3
77
+ when 83..86 then 3.0
78
+ when 80..82 then 2.7
79
+ when 77..79 then 2.3
80
+ when 73..76 then 2.0
81
+ when 70..72 then 1.7
82
+ when 67..69 then 1.3
83
+ when 63..66 then 1.0
84
+ when 60..62 then 0.7
85
+ else 0.0
86
+ end
87
+ end
88
+
89
+ # Convert a GPA to a letter grade
90
+ # @param gpa [Float] the GPA value
91
+ # @return [String] the letter grade
92
+ def gpa_to_letter(gpa)
93
+ case gpa
94
+ when 3.85..4.0 then "A"
95
+ when 3.5...3.85 then "A-"
96
+ when 3.15...3.5 then "B+"
97
+ when 2.85...3.15 then "B"
98
+ when 2.5...2.85 then "B-"
99
+ when 2.15...2.5 then "C+"
100
+ when 1.85...2.15 then "C"
101
+ when 1.5...1.85 then "C-"
102
+ when 1.15...1.5 then "D+"
103
+ when 0.85...1.15 then "D"
104
+ when 0.5...0.85 then "D-"
105
+ else "F"
106
+ end
107
+ end
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cgpa_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - GPA Calculator Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-04-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Calculate semester GPA, cumulative GPA (CGPA), and convert between percentage
14
+ and GPA. Supports 4.0 and 5.0 grading scales used by universities worldwide.
15
+ email:
16
+ - contact@gpacalc.app
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/cgpa_calculator.rb
24
+ homepage: https://gpacalc.app
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://gpacalc.app
29
+ source_code_uri: https://github.com/gpacalcteam/cgpa-calculator-ruby
30
+ changelog_uri: https://github.com/gpacalcteam/cgpa-calculator-ruby/blob/main/CHANGELOG.md
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.6.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.0.3.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: A lightweight Ruby library for calculating GPA, CGPA, and SGPA.
50
+ test_files: []