Skip to content

Instantly share code, notes, and snippets.

@kevlarr

kevlarr/apartment.rb

Last active May 4, 2016
Embed
What would you like to do?
Ruby Mixins

Hey, look at me!

This is a small little module demonstrating Ruby mixins with modules.

How to Use

Don't use this gist, actually.. Not using it is that easy!

class Apartment < Dwelling
include Holder
attr_accessor :rent
def initialize(address, rent, roommate_capacity)
super(address)
@rent = rent
set_capacity(roommate_capacity)
end
def roommates
holdables
end
def add_roommate(occupant)
add_holdable(occupant)
end
end
class Dwelling
attr_reader :address
def initialize(address)
@address = address
end
end
module Holder
def set_capacity(capacity)
@capacity = capacity
end
def full?
holdables.size >= capacity
end
private
attr_reader :capacity
def holdables
@holdables ||= []
end
def add_holdable(holdable)
holdables << holdable
end
end
class Truck
include Holder
def initialize(box_capacity)
set_capacity(box_capacity)
end
def boxes
holdables
end
def add_box(box)
add_holdable(box)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment