Skip to content

Instantly share code, notes, and snippets.

@LNA
Last active August 29, 2015 14:15
Show Gist options
  • Save LNA/79087e87143965dea875 to your computer and use it in GitHub Desktop.
Save LNA/79087e87143965dea875 to your computer and use it in GitHub Desktop.
Polymorphism
vogue_magazine = Magazine.new(vogue)
self_magazine = Magazine.new(self)
# How many pages are there in vogue magazine?
vogue_magazine.pages
# How many pages are there in self magazine?
self_magazine.pages
class Magazine
def initialize(name)
@magazine = name
end
def pages
@magazine.pages
end
def cover_story
@magazine.cover_story
end
end
class SelfMagazine
def initialize(self)
@self = self
end
def pages
@self.pages
end
def cover_story
@self.cover_story
end
end
class VogueMagazine
def initialize(vouge)
@vogue = vogue
end
def pages
@vogue.pages
end
def cover_story
@vogue.cover_story
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment