Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created January 30, 2014 17:06
Show Gist options
  • Save trikitrok/3b31cef89cabe279e7f3 to your computer and use it in GitHub Desktop.
Save trikitrok/3b31cef89cabe279e7f3 to your computer and use it in GitHub Desktop.
Quacking in Ruby
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