Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created June 17, 2009 17:46
Show Gist options
  • Save tenderlove/131378 to your computer and use it in GitHub Desktop.
Save tenderlove/131378 to your computer and use it in GitHub Desktop.
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