Created
January 30, 2014 17:06
-
-
Save trikitrok/3b31cef89cabe279e7f3 to your computer and use it in GitHub Desktop.
Quacking in Ruby
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 Duck | |
def initialize(how_to_quack) | |
@how_to_quack = how_to_quack | |
end | |
def how_to_quack=(how_to_quack) | |
@how_to_quack = how_to_quack | |
end | |
def quack | |
@how_to_quack.call | |
end | |
end | |
just_quacking = lambda {puts "Quack!\n"} | |
squeaking = lambda {puts "Squeak!\n"} | |
mute_quacking = lambda {puts "<< Silence >>\n"} | |
duck = Duck.new(just_quacking) | |
duck.quack | |
duck.how_to_quack = squeaking | |
duck.quack | |
duck.how_to_quack = mute_quacking | |
duck.quack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment