
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
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.
gem 'calcpace', '~> 1.5.5'
Then run:
bundle install
gem install calcpace
Before performing any calculations or conversions, create a new instance of Calcpace:
require 'calcpace'
calculate = Calcpace.new
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:
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.
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 Unit | Description |
---|---|
:km_to_mi | Kilometers to Miles |
:mi_to_km | Miles to Kilometers |
:nautical_mi_to_km | Nautical Miles to Kilometers |
:km_to_nautical_mi | Kilometers to Nautical Miles |
:meters_to_km | Meters to Kilometers |
:km_to_meters | Kilometers to Meters |
:meters_to_mi | Meters to Miles |
:mi_to_meters | Miles to Meters |
:m_s_to_km_h | Meters per Second to Kilometers per Hour |
:km_h_to_m_s | Kilometers per Hour to Meters per Second |
:m_s_to_mi_h | Meters per Second to Miles per Hour |
:mi_h_to_m_s | Miles per Hour to Meters per Second |
:m_s_to_feet_s | Meters per Second to Feet per Second |
:feet_s_to_m_s | Feet per Second to Meters per Second |
:km_h_to_mi_h | Kilometers per Hour to Miles per Hour |
:mi_h_to_km_h | Miles 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
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
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
To run the tests, clone the repository and run:
bundle exec rake
The tests are run using Ruby versions from 2.7.8 to 3.4.2, as specified in the .github/workflows/test.yml
file.
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.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that calcpace demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.