Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 3, 2024
1 parent 0923354 commit f91cbb5
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/haversine/distance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,23 @@ module Haversine
EARTH_RADIUS = 6371008.8

FACTORS = {
kilometers: EARTH_RADIUS / 1000,
meters: EARTH_RADIUS,
miles: EARTH_RADIUS / 1609.344,
feet: EARTH_RADIUS * 3.28084,
nauticalmiles: EARTH_RADIUS / 1852,
kilometers: EARTH_RADIUS / 1000,
meters: EARTH_RADIUS,
miles: EARTH_RADIUS / 1609.344,
feet: EARTH_RADIUS * 3.28084,
nautical_miles: EARTH_RADIUS / 1852,
}

property distance

def initialize(@distance : Number)
end

def to_kilometers : Number
@distance * FACTORS[:kilometers]
end

def to_meters : Number
@distance * FACTORS[:meters]
end

def to_miles : Number
@distance * FACTORS[:miles]
end

def to_nautical_miles : Number
@distance * FACTORS[:nauticalmiles]
end

def to_feet : Number
@distance * FACTORS[:feet]
end
{% for factor in ["meters", "kilometers", "miles", "nautical_miles", "feet"] %}
def to_{{factor.id}} : Number
@distance * FACTORS[:{{factor.id}}]
end
{% end %}

def <=>(other : Haversine::Distance)
distance <=> other.distance
Expand Down

0 comments on commit f91cbb5

Please sign in to comment.