Skip to content

Instantly share code, notes, and snippets.

@amcaplan
Last active August 29, 2015 14:01
Show Gist options
  • Save amcaplan/992b11955e20a05c26ab to your computer and use it in GitHub Desktop.
Save amcaplan/992b11955e20a05c26ab to your computer and use it in GitHub Desktop.
Final round of animals
class Animal
@animals = []
LEGS = 4
class << self
attr_reader :animals
alias :all :animals
end
def initialize(args={})
Animal.animals << self
after_initialize(args)
end
def after_initialize(args)
end
def legs
self.class::LEGS
end
end
class Octopus < Animal
@animals = []
LEGS = 8
def after_initialize(args={})
self.class.animals << self
end
end
class Llama < Animal
@animals = []
def after_initialize(args={})
self.class.animals << self
end
end
Octopus.new.legs # => 8
Llama.new.legs # => 4
Animal.all # => [#<Octopus:0x000001010ef220>,#<Llama:0x000001010a6868>]
Octopus.all # => [#<Octopus:0x000001010ef220>]
Llama.all # => [#<Llama:0x000001010a6868>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment