Last active
August 29, 2015 14:19
-
-
Save workmad3/688c71df61f4fb764316 to your computer and use it in GitHub Desktop.
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 Person | |
attr_reader :factory | |
attr_reader :name | |
def initialize(factory, name) | |
@factory = factory | |
@name = name | |
end | |
def coworkers | |
@factory.list | |
end | |
def coworkers_count | |
# the total size, minus yourself | |
coworkers.size - 1 | |
end | |
end | |
class PersonFactory | |
attr_reader :list | |
def initialize | |
@list = [] | |
end | |
private def add(person) | |
@list << person | |
person | |
end | |
def create(*args) | |
add Person.new(self, *args) | |
end | |
end | |
icedragons_factory = PersonFactory.new | |
workmad3_factory = PersonFactory.new | |
jr = icedragons_factory.create 'IceDragonjr' | |
icedragons_factory.create 'SomeDude' | |
icedragons_factory.create 'Bacon' | |
icedragons_factory.create 'ExtraDude' | |
wjr = workmad3_factory.create 'Workmad3jr' | |
workmad3_factory.create 'aPancake' | |
workmad3_factory.create 'TheGreatBacon' | |
jr.coworkers_count #=> 3 | |
wjr.coworkers_count #=> 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment