Newbie: How to model this?

Please be kind, this is probably a completely dumb newbie question,
but I just finished reading the Thomas/Hunt book and have basically
no previous experience with truely object-oriented programming:

How do I do the following in a elegant fashion?

I want to work with an object describing geographical coordinates
as you can read them from a GPS.

At least I need the longitude, latitude and height of the point
as an instance variable, as far as I can see. So far so good:

class Location
  def initialize(lon,lat,hgt)
    @lon = lon
    @lat = lat
    @hgt = hgt
  end
end

Latitude and longitude are basically angles, which can be described
in degrees, minutes and seconds or decimal degrees, and a number
of other formats.

I want methods like myPoint.dmsdegrees giving just the degrees
part of myPoint in degree, minute, second format. These should be
"virtual attributes" if I understand Thomas/Hunt right.

How can I define this in the most elegant fashion? I cannot let Location
inherit this from a base class "Angle" with all these conversion
methods, can I, because a Location is not a Angle, at least not just
a Angle.

Of course I could just code these conversion methods twice (once for
lat, once for lon) for each conversion in the body of the class
"Location". However I think there *must* be a better method than
this copy&paste techniqe.

TIA
/ralph

Ralph Aichinger wrote:

class Location
def initialize(lon,lat,hgt)
   @lon = lon
   @lat = lat
   @hgt = hgt
end
end

I want methods like myPoint.dmsdegrees giving just the degrees
part of myPoint in degree, minute, second format. These should be
"virtual attributes" if I understand Thomas/Hunt right.

How can I define this in the most elegant fashion? I cannot let Location
inherit this from a base class "Angle" with all these conversion
methods, can I, because a Location is not a Angle, at least not just
a Angle.

I would do it like this (or similar):

    class Location
        attr_accessor :lon, :lat, :hgt
        def initialize(lon,lat,hgt)
            @lon = DegreeValue.new(lon)
            @lat = DegreeValue.new(lat)
            @hgt = FootValue.new(hgt)
        end
    end

    class DegreeValue < Float
        def dms
            # Some calculation
            [ degrees, minutes, seconds ]
        end
    end

Alternatively you could add Numeric#dms.

···

--
My email address is malte at gmx-topmail.de

Thanks a lot! As stupid as it sounds, that was the part I
missed. Not *having* to declare types somehow confused
me ; )

Thanks for the fast help!

/ralph

···

Malte Milatz <malteDELETETHIS@gmx-topmail.de> wrote:

           @lon = DegreeValue.new(lon)