Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created March 26, 2016 07:42
Show Gist options
  • Save igaiga/56442d8dd0fe774155c0 to your computer and use it in GitHub Desktop.
Save igaiga/56442d8dd0fe774155c0 to your computer and use it in GitHub Desktop.
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