Created
February 11, 2020 04:48
-
-
Save harrisonmalone/05244a2768f1adc408026b41dae12347 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
| # this is an example of composition, we're initializing an instance of a class inside the parent | |
| class Home | |
| attr_reader :navbar | |
| def initialize | |
| @links = [] | |
| @navbar = Navbar.new | |
| end | |
| end | |
| class Navbar | |
| def render | |
| p "this is a navbar" | |
| end | |
| end | |
| home = Home.new | |
| home.navbar.render | |
| # this is inheritance where we're defining a method in the parent and then inheriting it for the child | |
| class Home | |
| attr_reader :navbar | |
| def initialize | |
| @links = [] | |
| end | |
| def render(type) | |
| if type == "navbar" | |
| p "this is a navbar" | |
| else | |
| p "no type passed!" | |
| end | |
| end | |
| end | |
| class Navbar < Home | |
| def initialize | |
| super() | |
| end | |
| end | |
| navbar = Navbar.new | |
| p navbar | |
| navbar.render("navbar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment