Skip to content

Instantly share code, notes, and snippets.

@omedale
Created April 22, 2019 17:18
Show Gist options
  • Select an option

  • Save omedale/a6d54e00efedf61b6f9fe384fabe379d to your computer and use it in GitHub Desktop.

Select an option

Save omedale/a6d54e00efedf61b6f9fe384fabe379d to your computer and use it in GitHub Desktop.
Composition - OOP
# when a class uses another object to provide some or all of its functionality
class Movement
def step
puts "stepping"
end
def crawl
puts "crawling"
end
end
class Human
def initialize
@movement = Movement.new
end
def move
@movement.step
end
end
class Raccoon
def initialize
@movement = Movement.new
end
def move
@movement.crawl
end
end
human = Human.new
raccoon = Raccoon.new
human.move
raccoon.move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment