Skip to content

Instantly share code, notes, and snippets.

@mikebsg01
Created October 18, 2022 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebsg01/79126e62426cd1a084e62884e283694b to your computer and use it in GitHub Desktop.
Save mikebsg01/79126e62426cd1a084e62884e283694b to your computer and use it in GitHub Desktop.
OOP with Accessors in Ruby - Lamborghini Example
# 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