ruby -- inherit from **another** class other than actual Parent class
I've been doing some 'monkey-patching' (ahem excuse me superman-patching),
like so, adding the below code and more to file(s) in my
"#{Rails.root}/initializers/" folder:
module RGeo
module Geographic
class ProjectedPointImpl
def to_s
coords = self.as_text.split("(").last.split(")").first.split(" ")
"#{coords.last}, #{coords.first}"
end# of to_s
def google_link
url = "https://maps.google.com/maps?hl=en&q=#{self.for_google}"
end
end# of ProjectedPointImpl class
end# of Geographic module
end
I ended up realizing that there were two different _Point_ instances which
I wanted to utilize these methods on (both were strings with the same
formatting, i.e. Well-Known Text (WKT)), and added an exact copy of the
above two methods to a certain RGeo::Geos::CAPIPointImpl class space.
I then, in my youthful, unexperienced way, after thinking about DRY (don't
repeat yourself) principles, proceeded to create an ad hoc class which I
presumed I might be able to inherit from for both
class Arghhh
def to_s
coords = self.as_text.split("(").last.split(")").first.split(" ")
"#{coords.last}, #{coords.first}"
end# of to_s
def google_link
url = "https://maps.google.com/maps?hl=en&q=#{self.for_google}"
end
end
and told my classes to inherit from it i.e.: ProjectedPointImpl < Arghhh
I was promptly responded to by ruby with this error when I stopped and
then tried to reload my rails console:
`<module:Geos>': superclass mismatch for class CAPIPointImpl (TypeError)
...
I think my naivete in trying to get CAPIPointImpl (in this case) to
inherit from another class than its parent also highlights my ignorance on
this subject excellently.
What methods might I be able to use to practically graft extra shared
methods onto two classes coming from otherwise separate parents? Does ruby
allow for these types of abstract exceptions? I apologize for my
ignorance, but I'm completely self-taught
No comments:
Post a Comment