Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created February 11, 2020 04:48
Show Gist options
  • Select an option

  • Save harrisonmalone/05244a2768f1adc408026b41dae12347 to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/05244a2768f1adc408026b41dae12347 to your computer and use it in GitHub Desktop.
# 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