-
-
Save amcaplan/992b11955e20a05c26ab to your computer and use it in GitHub Desktop.
Final round of animals
This file contains 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 | |
@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