Created
October 9, 2010 10:16
-
-
Save tatey/618087 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
# Converts degrees, minutes and seconds to decimal degrees | |
# | |
# "52°12′17.0″N".to_degrees # => 52.2047222222222 | |
# "27 28 05S".to_degrees # => -27.4680555555556 | |
# "Invalid".to_degrees # => 0.0 | |
# | |
# Returns Float | |
def to_degrees | |
self =~ /^(\d+)[^\d]+(\d+)[^\d]+([\d\.]+)[^NESW]*([NESW])/i | |
deg = $1.to_f + ($2.to_f / 60) + ($3.to_f / 3600) | |
$4 =~ /[SW]/i ? -deg : +deg | |
end | |
end | |
puts "52°12′17.0″N".to_degrees | |
puts "27 28 05S".to_degrees |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment