Created
October 18, 2022 17:23
-
-
Save mikebsg01/79126e62426cd1a084e62884e283694b to your computer and use it in GitHub Desktop.
OOP with Accessors in Ruby - Lamborghini Example
This file contains 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
# Useful content: https://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby | |
class MyObject | |
attr_reader :model | |
def initialize arg | |
puts "This is an object" | |
puts "Argument obtained: #{arg}" | |
@model = arg | |
end | |
end | |
class Car < MyObject | |
def run | |
puts "This #{@model} can run" | |
end | |
def run_fast | |
puts "This #{@model} can run really fast" | |
end | |
end | |
class Lamborghini < Car | |
def run_fast | |
puts "This #{@model} can run really really fast" | |
end | |
end | |
l = Lamborghini.new(:aventador) | |
l.run | |
l.run_fast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment