From ad9aa5368fb6a34294fb9cd4e48aed3bae244dbf Mon Sep 17 00:00:00 2001 From: tenderlove Date: Tue, 2 Nov 2010 21:24:54 +0000 Subject: [PATCH] * ext/psych/lib/psych/visitors/visitor.rb (accept): switch to a dispatch cache rather than case / when statement. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ext/psych/lib/psych/visitors/visitor.rb | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 55dbb0b81d..f3848f2c8a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Nov 2 22:47:08 2010 Aaron Patterson + + * ext/psych/lib/psych/visitors/visitor.rb (accept): switch to + a dispatch cache rather than case / when statement. + Tue Nov 2 21:46:52 2010 Kouhei Sutou * NEWS: fix a typo. diff --git a/ext/psych/lib/psych/visitors/visitor.rb b/ext/psych/lib/psych/visitors/visitor.rb index 3471c43dfa..0e33128f01 100644 --- a/ext/psych/lib/psych/visitors/visitor.rb +++ b/ext/psych/lib/psych/visitors/visitor.rb @@ -11,16 +11,17 @@ module Psych end def accept target - case target - when Psych::Nodes::Scalar then visit_Psych_Nodes_Scalar target - when Psych::Nodes::Mapping then visit_Psych_Nodes_Mapping target - when Psych::Nodes::Sequence then visit_Psych_Nodes_Sequence target - when Psych::Nodes::Alias then visit_Psych_Nodes_Alias target - when Psych::Nodes::Document then visit_Psych_Nodes_Document target - when Psych::Nodes::Stream then visit_Psych_Nodes_Stream target - else - raise "Can't handle #{target}" - end + visit target + end + + private + + DISPATCH = Hash.new do |hash, klass| + hash[klass] = "visit_#{klass.name.gsub('::', '_')}" + end + + def visit target + send DISPATCH[target.class], target end end end