Class: String

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

Overview

Extends String to to_dec_degrees, add to_r and to_d

Instance Method Summary collapse

Instance Method Details

#to_dec_degreesFloat

string expected to be degrees, returns decimal degrees. common forms are S37 001’7.5”, 37 001’7.5”S , -37 001’7.5”, -37 0 1.512’. -37.01875 0, 37 001’.512S, S37 001’.512, …

Returns:

  • (Float)

    angle in decimal degrees



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/core_extensions.rb', line 42

def to_dec_degrees
  # reorder 37 001'.512S, S37 001'.512 into 37 001.512'S, S37 001.512' respectively
  s = self.gsub(/([0-9])(')\.([0-9]+)/, '\1.\3\2')
  # add in minutes and seconds to get 3 values 'deg 0 0'from  S37 0, 37 0S
  s.gsub!(/^([^0-9.\-]*)([0-9\-.]+)([^0-9\-.]*)$/, '\1\2\3 0 0\5')
  # add in seconds get 3 values 'deg min 0' from  S37 01.512',  37 01.512'S
  s.gsub!(/^([^0-9.\-]*)([0-9\-.]+)([^0-9\-.]+)([0-9\-.]+)([^0-9\-.]*)$/, '\1\2\3\4 0\5')

  # look for anything of the form S37 001'7.5'', S37 01.512', S37.01875 0, ...
  s.scanf('%[NSEW]%f%[^0-9-]%f%[^0-9-]%f') do |direction, deg, _sep1, min, _sep2, sec|
    return Angle.decimal_deg( deg,  min,  sec,  direction)
  end

  # look for anything of the form 37 001'7.5''S , -37 001'7.5'', -37 0 1.512'. -37.01875 0, ...
  s.scanf('%f%[^0-9-]%f%[^0-9-]%f%[^NSEW]%[NSEW]') do |deg, _sep1, min, _sep2, sec, _sep3, direction|
    return Angle.decimal_deg( deg,  min,  sec,  direction)
  end
end

#to_degrees(mod = false) ⇒ Float Also known as: to_deg

Convert String number in Radians to Degrees

Parameters:

  • mod (true, false) (defaults to: false)

    Optional argument mod == true, then applies % 360

Returns:

  • (Float)

    degrees



64
65
66
# File 'lib/core_extensions.rb', line 64

def to_degrees(mod = false)
  return self.to_f.to_degrees(mod)
end

#to_radians(mod = false) ⇒ Float Also known as: to_rad

Converts string degrees to to_decimal_degrees, then to Radians

Parameters:

  • mod (true, false) (defaults to: false)

    Optional argument mod == true, then applies % Math::PI

Returns:

  • (Float)

    radians



71
72
73
# File 'lib/core_extensions.rb', line 71

def to_radians(mod = false)
  return self.to_dec_degrees.to_radians(mod)
end