Class: Haversine::Distance

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/haversine/distance.rb

Constant Summary collapse

GREAT_CIRCLE_RADIUS_MILES =
3956
GREAT_CIRCLE_RADIUS_KILOMETERS =

some algorithms use 6367

6371
GREAT_CIRCLE_RADIUS_FEET =
GREAT_CIRCLE_RADIUS_MILES * 5280
GREAT_CIRCLE_RADIUS_METERS =
GREAT_CIRCLE_RADIUS_KILOMETERS * 1000
GREAT_CIRCLE_RADIUS_NAUTICAL_MILES =
GREAT_CIRCLE_RADIUS_MILES / 1.15078

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(great_circle_distance) ⇒ Distance

Returns a new instance of Distance.



13
14
15
# File 'lib/haversine/distance.rb', line 13

def initialize(great_circle_distance)
  @great_circle_distance = great_circle_distance
end

Instance Attribute Details

#great_circle_distanceObject (readonly)

Returns the value of attribute great_circle_distance.



11
12
13
# File 'lib/haversine/distance.rb', line 11

def great_circle_distance
  @great_circle_distance
end

Instance Method Details

#<=>(other) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/haversine/distance.rb', line 42

def <=>(other)
  if other.respond_to? :great_circle_distance # it's duck if it quacks
    great_circle_distance <=> other.great_circle_distance
  else
    return nil # spitting out nil when objects are not comparable
  end
end

#to_feetObject Also known as: to_ft



32
33
34
# File 'lib/haversine/distance.rb', line 32

def to_feet
  @great_circle_distance * GREAT_CIRCLE_RADIUS_FEET
end

#to_kilometersObject Also known as: to_km



22
23
24
# File 'lib/haversine/distance.rb', line 22

def to_kilometers
  @great_circle_distance * GREAT_CIRCLE_RADIUS_KILOMETERS
end

#to_metersObject Also known as: to_m



27
28
29
# File 'lib/haversine/distance.rb', line 27

def to_meters
  @great_circle_distance * GREAT_CIRCLE_RADIUS_METERS
end

#to_milesObject Also known as: to_mi



17
18
19
# File 'lib/haversine/distance.rb', line 17

def to_miles
  @great_circle_distance * GREAT_CIRCLE_RADIUS_MILES
end

#to_nautical_milesObject Also known as: to_nm



37
38
39
# File 'lib/haversine/distance.rb', line 37

def to_nautical_miles
  @great_circle_distance * GREAT_CIRCLE_RADIUS_NAUTICAL_MILES
end