# hardcoded behaviour
class Cow
def speak
'Mooooo!'
end
end
# creature dependency example
# with and without singletons
class Animal
def initialize(source)
@source = source
end
def speak_class
@source.speak
end
def speak_instance
@source.new.speak
end
end
class Cow
def speak
"Moooo!"
end
end
class Bird
def self.speak
"Tweet!"
end
end
#Animal.new(Cow).speak_class
#=> NoMethodError: undefined method 'speak' for Cow:Class
#
#Animal.new(Cow).speak_instance
#=> "Moooo!"
#
#Animal.new(Bird).speak_class
#=> "Tweet!"
#
#Animal.new(Bird).speak_instance
#=> NoMethodError: undefined method 'speak' for #<Bird:0x00000000ff96c8>
Last active
December 25, 2015 16:19
-
-
Save zloncar/7004625 to your computer and use it in GitHub Desktop.
Dependency injection (+ singletons)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment