Skip to content

Instantly share code, notes, and snippets.

@anewusername1
Created January 18, 2012 21:51
Show Gist options
  • Save anewusername1/1635999 to your computer and use it in GitHub Desktop.
Save anewusername1/1635999 to your computer and use it in GitHub Desktop.
# Each of the following classes visit the objects, doing something different
# each time.
class CarElementPrintVisitor
def visit(subject)
puts "Visiting: %s" % subject.class.to_s
end
end
class CarElementDoVisitor
def visit(subject)
puts "Do some other visitation: %s" % subject.class.to_s
end
end
c = Car.new
c.accept(CarElementPrintVisitor.new) # pass an object that will be visited
c.accept(CarElementDoVisitor.new) # same here
# OUTPUT
# Visiting: Wheel
# Visiting: Wheel
# Visiting: Wheel
# Visiting: Wheel
# Visiting: Body
# Visiting: Engine
# Visiting: Car
# Do some other visitation: Wheel
# Do some other visitation: Wheel
# Do some other visitation: Wheel
# Do some other visitation: Wheel
# Do some other visitation: Body
# Do some other visitation: Engine
# Do some other visitation: Car
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment