Created
June 17, 2009 17:46
-
-
Save tenderlove/131378 to your computer and use it in GitHub Desktop.
This file contains 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
module Visitable | |
def accept object | |
object.visit self | |
end | |
end | |
class Visitor | |
def visit thing | |
send(:"visit_#{thing.class.name}", thing) | |
end | |
end | |
class Object; include Visitable; end | |
class Doubler < Visitor | |
def visit_Array array | |
array.map { |thing| thing.accept self } | |
end | |
def visit_Fixnum num | |
num * 2 | |
end | |
end | |
p 1.accept Doubler.new | |
p [1, [2, 3]].accept Doubler.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment