Created
March 26, 2016 07:42
-
-
Save igaiga/56442d8dd0fe774155c0 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 Cat | |
def hello | |
"nya" | |
end | |
end | |
class Dog | |
def hello | |
"wan" | |
end | |
end | |
class Cage | |
def initialize(animal = nil) | |
@animals = [] | |
@animals << animal if animal | |
end | |
def add(animal = nil) | |
return self unless animal | |
@animals << animal | |
self | |
end | |
def show | |
# @animals.each do |animal| | |
# p animal.hello | |
# end | |
# [cat, dog] #=> ["nya", "wan] | |
#p @animals.map{ |animal| animal.hello }.to_a | |
p @animals.map(&:hello).to_a | |
self | |
end | |
end | |
Cage.new.add(Cat.new).add(Dog.new).show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment