Skip to content

Instantly share code, notes, and snippets.

@kadru
Last active April 10, 2025 20:57
Show Gist options
  • Save kadru/61ec10f77bba4143569e02fa6447ed4d to your computer and use it in GitHub Desktop.
Save kadru/61ec10f77bba4143569e02fa6447ed4d to your computer and use it in GitHub Desktop.
inheritance example
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
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
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
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
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
let animal = {
eats: true
};
let rabbit = {
jumps: true
};
rabbit.__proto__ = animal;
console.log(rabbit.eats);
console.log(animal.eats);
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
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.