Module: PaceCalculator
- Included in:
- Calcpace
- Defined in:
- lib/calcpace/pace_calculator.rb
Overview
Module for calculating race times and paces for standard distances
This module provides convenience methods for calculating finish times and paces for common race distances like 5K, 10K, half-marathon, marathon, and 100K.
Constant Summary collapse
- RACE_DISTANCES =
Standard race distances in kilometers
{ '5k' => 5.0, '10k' => 10.0, 'half_marathon' => 21.0975, 'marathon' => 42.195, '100k' => 100.0, '1mile' => Converter::Distance::MI_TO_KM, '5mile' => 5 * Converter::Distance::MI_TO_KM, '10mile' => 10 * Converter::Distance::MI_TO_KM }.freeze
- SAME_DISTANCE_TOLERANCE_RATIO =
Relative window under which two distances count as the same race. It exists only to absorb floating-point noise, never to call two different distances equal — 10.0 km and 10.2 km are a legitimate prediction, not a mistake
1e-9
Instance Method Summary collapse
-
#list_races ⇒ Hash
Lists all available standard race distances.
-
#race_pace(target_time, race) ⇒ Float
Calculates the required pace per kilometer to finish a race in a target time.
-
#race_pace_clock(target_time, race) ⇒ String
Calculates the required pace and returns it as a clock time string.
-
#race_time(pace_per_km, race) ⇒ Float
Calculates the finish time for a race given a pace per kilometer.
-
#race_time_clock(pace_per_km, race) ⇒ String
Calculates the finish time for a race and returns it as a clock time string.
Instance Method Details
#list_races ⇒ Hash
Lists all available standard race distances
94 95 96 |
# File 'lib/calcpace/pace_calculator.rb', line 94 def list_races RACE_DISTANCES.dup end |
#race_pace(target_time, race) ⇒ Float
Calculates the required pace per kilometer to finish a race in a target time
68 69 70 71 72 73 |
# File 'lib/calcpace/pace_calculator.rb', line 68 def race_pace(target_time, race) distance = race_distance(race) time_seconds = target_time.is_a?(String) ? convert_to_seconds(target_time) : target_time check_positive(time_seconds, 'Time') time_seconds / distance end |
#race_pace_clock(target_time, race) ⇒ String
Calculates the required pace and returns it as a clock time string
84 85 86 |
# File 'lib/calcpace/pace_calculator.rb', line 84 def race_pace_clock(target_time, race) convert_to_clocktime(race_pace(target_time, race)) end |
#race_time(pace_per_km, race) ⇒ Float
Calculates the finish time for a race given a pace per kilometer
37 38 39 40 41 42 |
# File 'lib/calcpace/pace_calculator.rb', line 37 def race_time(pace_per_km, race) distance = race_distance(race) pace_seconds = pace_per_km.is_a?(String) ? convert_to_seconds(pace_per_km) : pace_per_km check_positive(pace_seconds, 'Pace') distance * pace_seconds end |
#race_time_clock(pace_per_km, race) ⇒ String
Calculates the finish time for a race and returns it as a clock time string
54 55 56 |
# File 'lib/calcpace/pace_calculator.rb', line 54 def race_time_clock(pace_per_km, race) convert_to_clocktime(race_time(pace_per_km, race)) end |