[RUBY] Calculate and display grades

It is a code that calculates and displays the grade from the year of birth and the month of birth. It's irregular, so I think it can vary from person to person, but for many people this should be the case.

Grades are calculated with April as the delimiter. Pass the year and month of birth in birth_year and birth_month, and the year and month of calculation in judge_year and judge_month.

Argument example

code

 def grade(birth_year, birth_month, judge_year, judge_month)
    judge_year -= 1 if judge_month < 4
    birth_year -= 1 if birth_month < 4
    year = judge_year - birth_year

    return "Preschool" if year <= 3
    return case year
           when 4 then
             "Younger"
           when 5 then
             "All year round"
           when 6 then
             "Senior"
           when 7 then
             "1st grade"
           when 8 then
             "2nd grade"
           when 9 then
             "3rd grade"
           when 10 then
             "4th grade"
           when 11 then
             "5th grade"
           when 12 then
             "6th grade"
           when 13 then
             "1st year of junior high school"
           when 14 then
             "2nd year of junior high school"
           when 15 then
             "Junior high school 3rd grade"
           when 16 then
             "1st year high school"
           when 17 then
             "2nd year high school"
           when 18 then
             "3rd year high school"
           else
             "graduate"
           end
  end

Calculation flow

Consider one year from April as the year. The grade is determined by the age at the beginning of the year (April 1st). For those born early (born January-March), birth_year is one before, so subtract 1 from birth_year. If the calculation date is January-March, the calculation will be for the year one year before the number specified by judge_year, so subtract 1 from judge_year. The difference between the years is 3 years if you are younger before school and 6 years if you are 1st grade. In other words, the difference between the years is 4 years for younger children and 7 years for elementary school students. Returns a string to match this number.

Other

Others were trying to do the same

Recommended Posts

Calculate and display grades
Format and display numbers
Save and display multiple images