Class: Speedify

Inherits:
Object
  • Object
show all
Defined in:
lib/speedify.rb

Class Method Summary collapse

Class Method Details

.distance_for_time(time_hours, speed_kmh) ⇒ Object

Calculate distance given speed time (in km/h) and time (in hours)

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/speedify.rb', line 21

def self.distance_for_time(time_hours, speed_kmh)
  raise ArgumentError, "Speed must be greater than zero(0)" if speed_kmh <= 0

  time_hours * speed_kmh
end

.speed_for_distance_and_time(distance_km, time_hours) ⇒ Object

Calculate speed given distance (in km) and time (in hours)

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/speedify.rb', line 28

def self.speed_for_distance_and_time(distance_km, time_hours)
  raise ArgumentError, "Time must be greater than Zero(0)" if time_hours <= 0

  distance_km / time_hours
end

.speed_to_kmh(speed_mps) ⇒ Object

Convert speed from m/s to km/h



8
9
10
# File 'lib/speedify.rb', line 8

def self.speed_to_kmh(speed_mps)
  (speed_mps * 3600.0) / 1000.0
end

.speed_to_mps(speed_kmh) ⇒ Object

Convert speed from km/h to m/s



3
4
5
# File 'lib/speedify.rb', line 3

def self.speed_to_mps(speed_kmh)
  (speed_kmh * 1000.0) / 3600.0
end

.time_for_distance(distance_km, speed_kmh) ⇒ Object

Calculate time given distance (in km) and speed(in km/h)

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/speedify.rb', line 13

def self.time_for_distance(distance_km, speed_kmh)
  raise ArgumentError, "Speed must be greater than zero(0)" if speed_kmh <= 0

  time_hours = distance_km / speed_kmh
  convert_hours_to_time_format(time_hours)
end