How about using a regular expression? Eg:
def latlong(dms_pair)
match = dms_pair.match(/(\d\d)º (\d\d)' (\d\d)'' (\d\d)º (\d\d)' (\d\d)''/)
latitude = match[1].to_f + match[2].to_f / 60 + match[3].to_f / 3600
longitude = match[4].to_f + match[5].to_f / 60 + match[6].to_f / 3600
{:latitude=>latitude, :longitude=>longitude}
end
Here's a more complex version that copes with negative coordinates:
def dms_to_degrees(d, m, s)
degrees = d
fractional = m / 60 + s / 3600
if d > 0
degrees + fractional
else
degrees - fractional
end
end
def latlong(dms_pair)
match = dms_pair.match(/(-?\d+)º (\d+)' (\d+)'' (-?\d+)º (\d+)' (\d+)''/)
latitude = dms_to_degrees(*match[1..3].map {|x| x.to_f})
longitude = dms_to_degrees(*match[4..6].map {|x| x.to_f})
{:latitude=>latitude, :longitude=>longitude}
end