Last active
August 29, 2015 14:10
-
-
Save GrooveStomp/784b2d4ec0b83df3e8b6 to your computer and use it in GitHub Desktop.
Ruby Dependency Injection
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 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