Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created September 28, 2012 15:16
Show Gist options
  • Save andredublin/3800467 to your computer and use it in GitHub Desktop.
Save andredublin/3800467 to your computer and use it in GitHub Desktop.
How to make this duck quack for both its data and behavior messages?
module Preparable
def chat
puts "Hi"
end
def speak
puts "there"
end
def say_again
puts "I said #{chat} #{speak}!!" #evaluation order is #thing, #speak, then #say_again??
end
end
#============
class Foo
include Preparable
attr_reader :bar, :baz
def prepare(*fooey)
fooey.each {|f| f.prepare_duck self}
end
# def speak
# puts "foo"
# end
end
#============
class Bar
include Preparable
#Implement duck interface
def prepare_duck foo
foo_bar = foo.bar
# return foo_bar
end
# def speak
# puts "bar"
# end
end
#============
class Baz
include Preparable
#Implement duck interface
def prepare_duck foo
foo_baz = foo.baz
# return foo_baz
end
# def speak
# puts "baz"
# end
end
f = Foo.new
f.prepare @bar = Bar.new, @baz = Baz.new
@bar.chat
@baz.chat
@bar.speak
@baz.speak
@bar.say_again
@baz.say_again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment