Skip to content

Instantly share code, notes, and snippets.

@mikepack
Last active August 29, 2015 14:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikepack/8ad38318971df9f9f7c3 to your computer and use it in GitHub Desktop.
Save mikepack/8ad38318971df9f9f7c3 to your computer and use it in GitHub Desktop.
5 Composition Techniques
class Animal
def run
puts 'running'
end
end
Animal.new.run #=> running
class Wolf < Animal
def follow_pack
puts 'following'
end
end
Wolf.new.follow_pack #=> following
Wolf.new.run #=>running
# How do we get the #follow_pack behavior in Dolphin?
class Dolphin < Animal, Wolf?
end
# SyntaxError: (irb):14: syntax error, unexpected ',', expecting ';' or '\n'
# class Dolphin < Animal, Wolf?
# ^
# from /Users/mikepack/.rvm/rubies/ruby-2.1.3/bin/irb:11:in `<main>'
# We could define a super class...
class Pack < Animal
def follow_pack
puts 'following'
end
end
class Dolphin < Pack
end
Dolphin.new.follow_pack #=> following
# This doesn't really make sense...
Pack.new.follow_pack #=> following
class Animal
def run
puts 'running'
end
end
# We can use this module to include behavior in multiple classes...
module Pack
def follow_pack
puts 'following'
end
end
class Wolf < Animal
include Pack
end
class Dolphin < Animal
include Pack
end
Wolf.new.follow_pack #=> following
module Hunter
def hunt
puts 'hunting'
end
end
class Wolf < Animal
# We can include multiple mixins
include Pack, Hunter
end
Wolf.new.hunt #=> hunting
Wolf.new.follow_pack #=> following
class Programmer
def program
puts 'doin my thang'
end
end
require 'delegate'
class Manager < SimpleDelegator
def make_charts
puts 'sales are up'
end
end
class Manager
def make_charts
puts 'sales are up'
end
end
p = Programmer.new
p.program #=> doin my thang
# SimpleDelegators will delegate any methods they don't have
# to the object you pass into .new
m = Manager.new(p)
m.make_charts #=> sales are up
m.program #=> doin my thang
m.__getobj__ #=> #<Programmer:0x007fcbc202efc8>
m.__getobj__ == p #=> true
class Manager
def make_charts
puts 'sales are up'
end
end
class ExecutivePresentation
def initialize
@manager = Manager.new
end
def start
@manager.make_charts
end
end
ExecutivePresentation.new.start #=> sales are up
# Say the executives ask the manager to also report about
# developer productivity, on the fly...
module Productivity
def recap_productivity
puts 'programmers are smarter'
end
end
class ExecutivePresentation
def initialize
@manager = Manager.new
end
def start
@manager.make_charts
@manager.extend Productivity
@manager.recap_productivity
end
end
ExecutivePresentation.new.start
# => sales are up
# => programmers are smarter
module Productivity
refine Manager do
def recap_productivity
puts 'programmers are smarter'
end
end
end
class ExecutivePresentation
using Productivity
def initialize
@manager = Manager.new
end
def start
@manager.make_charts
@manager.recap_productivity
end
end
ExecutivePresentation.new.start
# => sales are up
# => programmers are smarter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment