Skip to content

Instantly share code, notes, and snippets.

@GrooveStomp
Last active August 29, 2015 14:10
Show Gist options
  • Save GrooveStomp/784b2d4ec0b83df3e8b6 to your computer and use it in GitHub Desktop.
Save GrooveStomp/784b2d4ec0b83df3e8b6 to your computer and use it in GitHub Desktop.
Ruby Dependency Injection
class Rabbit
def initialize(color, size)
@color = color
@size = size
end
def hop
if @color == 'blue'
puts "hopped #{2 * @size} feet"
else
puts "hopped #{4 * @size} feet"
end
end
end
# With explicit dependency injection it instead looks like this:
class Rabbit
def hop(color, size)
if color == 'blue'
puts "hopped #{2 * size} feet"
else
puts "hopped #{4 * size} feet"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment