Last active
April 10, 2025 20:57
-
-
Save kadru/61ec10f77bba4143569e02fa6447ed4d to your computer and use it in GitHub Desktop.
inheritance example
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
class Driver | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
class Car | |
def initialize(model:, driver:) | |
@model = model | |
@driver = driver # Aggregation: Car has a Driver, but Driver exists independently | |
end | |
def info | |
"#{driver.name} is driving a #{model}." | |
end | |
end | |
car = Car.new(model: "Dodge Charger", driver: Driver.new(name: "Toretto")) | |
puts car.info |
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
class Computer | |
attr_accessor :display | |
attr_accessor :motherboard | |
attr_reader :drives | |
def initialize(display=:crt, motherboard=Motherboard.new, drives=[]) | |
@motherboard = motherboard | |
@drives = drives | |
@display = display | |
end | |
end | |
class ComputerBuilder | |
attr_reader :computer | |
def initialize | |
@computer = Computer.new | |
end | |
def turbo(has_turbo_cpu=true) | |
@computer.motherboard.cpu = TurboCPU.new | |
end | |
def display=(display) | |
@computer.display=display | |
end | |
def memory_size=(size_in_mb) | |
@computer.motherboard.memory_size = size_in_mb | |
end | |
def add_cd(writer=false) | |
@computer.drives << Drive.new(:cd, 760, writer) | |
end | |
def add_dvd(writer=false) | |
@computer.drives << Drive.new(:dvd, 4000, writer) | |
end | |
def add_hard_disk(size_in_mb) | |
@computer.drives << Drive.new(:hard_disk, size_in_mb, true) | |
end | |
end | |
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
class Engine | |
def start = "starting..." | |
end | |
class Car | |
def initialize(engine) | |
@engine = engine | |
end | |
def start = @engine.start | |
def drive = "driving..." | |
end | |
car = Car.new(Engine.new) | |
puts car.start | |
puts car.drive |
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
class Animal | |
def eat = "eating" | |
end | |
class Dog < Animal | |
def eat = super + " 🦴" | |
end | |
animal = Animal.new | |
dog = Dog.new | |
puts animal.eat | |
puts dog.eat |
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
module Villainable | |
def do_evil = "An evil thing 😈" | |
end | |
module Heronable | |
def do_good = "A good thing 😇" | |
end | |
class Actor | |
include Villainable | |
include Heronable | |
end | |
actor = Actor.new | |
puts actor.do_evil | |
puts actor.do_good |
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
let animal = { | |
eats: true | |
}; | |
let rabbit = { | |
jumps: true | |
}; | |
rabbit.__proto__ = animal; | |
console.log(rabbit.eats); | |
console.log(animal.eats); |
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
class Bicycle | |
attr_reader :size, :chain, :tire_size | |
def initialize(args={}) | |
@size = args[:size] | |
@chain = args[:chain] || default_chain | |
@tire_size = args[:tire_size] || default_tire_size | |
end | |
def default_chain # <- common default | |
'10-speed' | |
end | |
end | |
class RoadBike < Bicycle | |
# ... | |
def default_tire_size # <- subclass default | |
'23' | |
end | |
end | |
class MountainBike < Bicycle | |
# ... | |
def default_tire_size # <- subclass default | |
'2.1' | |
end | |
end |
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
class Bicycle | |
def initialize(args={}) | |
@size = args[:size] | |
@chain = args[:chain] || default_chain | |
@tire_size = args[:tire_size] || default_tire_size | |
post_initialize(args) # Bicycle both sends | |
end | |
def post_initialize(args) # and implements this | |
nil | |
end | |
# ... | |
end | |
class RoadBike < Bicycle | |
def post_initialize(args) # RoadBike can | |
@tape_color = args[:tape_color] # optionally | |
end # override it | |
# ... | |
end |
Comments are disabled for this gist.