Socket
Book a DemoInstallSign in
Socket

calcpace

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

calcpace

1.5.5
bundlerRubygems
Version published
Maintainers
1
Created
Source

Calcpace Gem Version

Calcpace is a Ruby gem designed for calculations and conversions related to distance and time. It can calculate velocity, pace, total time, and distance, accepting time in various formats, including HH:MM:SS. The gem supports conversion to 42 different units, including kilometers, miles, meters, and feet. It also provides methods to validate input.

Installation

Add to your Gemfile

gem 'calcpace', '~> 1.5.5'

Then run:

bundle install

Install the gem manually

gem install calcpace

Usage

Before performing any calculations or conversions, create a new instance of Calcpace:

require 'calcpace'

calculate = Calcpace.new

Calculate using Integers or Floats

Calcpace provides methods to calculate velocity, pace, total time, and distance. The methods are unit-agnostic, and the return value is a float. Here are some examples:

calculate.velocity(3625, 12275) # => 3.386206896551724
calculate.pace(3665, 12) # => 305.4166666666667
calculate.time(210, 12) # => 2520.0
calculate.distance(9660, 120) # => 80.5

Tip: Use the round method to round a float. For example:

calculate.velocity(3625, 12275).round(3) # => 3.386

Remember:

  • Velocity is the distance divided by the time (e.g., m/s or km/h).
  • Pace is the time divided by the distance (e.g., minutes/km or minutes/miles).
  • Total time is the distance divided by the velocity.
  • Distance is the velocity multiplied by the time.

Calculate using Clocktime

Calcpace also provides methods to calculate using clocktime (HH:MM:SS or MM:SS format string). The return value will be in seconds or clocktime, depending on the method called, except for checked_distance. Here are some examples:

# The return will be in the unit you input/seconds or seconds/unit you input
calculate.checked_velocity('01:00:00', 12275) # => 3.4097222222222223
calculate.checked_pace('01:21:32', 10) # => 489.2
calculate.checked_time('00:05:31', 12.6) # => 4170.599999999999

calculate.checked_distance('01:21:32', '00:06:27') # => 12.640826873385013

# The return will be in clocktime
calculate.clock_pace('01:21:32', 10) # => "00:08:09"
calculate.clock_velocity('01:00:00', 10317) # => "00:00:02"
calculate.clock_time('00:05:31', 12.6) # => "01:09:30"

Note: Using the clock methods may be less precise than using other methods due to conversions.

You can also use BigDecimal for more precise calculations. For example:

require 'bigdecimal'
calculate.checked_velocity('10:00:00', 10317).to_d # => #<BigDecimal:7f9f1b8b1d08,'0.2865833333 333333E1',27(36)>

To learn more about BigDecimal, check the documentation.

Convert Distances and Velocities

Use the convert method to convert a distance or velocity. The first parameter is the value to be converted, and the second parameter is the unit to which the value will be converted. The unit must be a string with the abbreviation of the unit. The gem supports 26 different units, including kilometers, miles, meters, knots, and feet.

Here are some examples:

converter.convert(10, :km_to_meters) # => 1000
converter.convert(10, :mi_to_km) # => 16.0934
converter.convert(1, :nautical_mi_to_km) # => 1.852
converter.convert(1, :km_h_to_m_s) # => 0.277778
converter.convert(1, :m_s_to_mi_h) # => 2.23694
Conversion UnitDescription
:km_to_miKilometers to Miles
:mi_to_kmMiles to Kilometers
:nautical_mi_to_kmNautical Miles to Kilometers
:km_to_nautical_miKilometers to Nautical Miles
:meters_to_kmMeters to Kilometers
:km_to_metersKilometers to Meters
:meters_to_miMeters to Miles
:mi_to_metersMiles to Meters
:m_s_to_km_hMeters per Second to Kilometers per Hour
:km_h_to_m_sKilometers per Hour to Meters per Second
:m_s_to_mi_hMeters per Second to Miles per Hour
:mi_h_to_m_sMiles per Hour to Meters per Second
:m_s_to_feet_sMeters per Second to Feet per Second
:feet_s_to_m_sFeet per Second to Meters per Second
:km_h_to_mi_hKilometers per Hour to Miles per Hour
:mi_h_to_km_hMiles per Hour to Kilometers per Hour

You can list all the available units here, or using list methods:

converter.list_all
converter.list_distance
converter.list_speed

Other Useful Methods

Calcpace also provides other useful methods:

converter = Calcpace.new
converter.convert_to_seconds('01:00:00') # => 3600
converter.convert_to_clocktime(3600) # => '01:00:00'
converter.converto_to_clocktime(100000) # => '1 03:46:40'
converter.check_time('01:00:00') # => nil

Errors

The gem now raises specific custom error classes for invalid inputs, allowing for more precise error handling. These errors inherit from Calcpace::Error.

  • Calcpace::NonPositiveInputError: Raised when a numeric input is not positive.
  • Calcpace::InvalidTimeFormatError: Raised when a time string is not in the expected HH:MM:SS or MM:SS format.

For example:

begin
  calculate.pace(945, -1)
rescue Calcpace::NonPositiveInputError => e
  puts e.message # => "Input must be a positive number"
end

begin
  calculate.checked_time('string', 10)
rescue Calcpace::InvalidTimeFormatError => e
  puts e.message # => "It must be a valid time in the XX:XX:XX or XX:XX format"
end

Testing

To run the tests, clone the repository and run:

bundle exec rake

Supported Ruby Versions

The tests are run using Ruby versions from 2.7.8 to 3.4.2, as specified in the .github/workflows/test.yml file.

Contributing

We welcome contributions to Calcpace! To contribute, clone this repository and submit a pull request. Please ensure that your code adheres to our style and includes tests where appropriate.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 28 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.