Skip to content

Instantly share code, notes, and snippets.

@djmetzle
Created September 18, 2019 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djmetzle/e54247043f97b1935ff7126311e36e5e to your computer and use it in GitHub Desktop.
Save djmetzle/e54247043f97b1935ff7126311e36e5e to your computer and use it in GitHub Desktop.
Class dispatch
class Foo
def quack
puts "quack"
end
end
class Bar
def bark
puts "woof"
end
end
class Dispatcher
def dispatch obj
puts "dispatch #{obj.class}"
case obj
when Foo
obj.quack
when Bar
obj.bark
else
raise "WaT"
end
end
end
f = Foo.new
b = Bar.new
x = Object.new
d = Dispatcher.new
d.dispatch f
d.dispatch b
d.dispatch x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment