Created
April 15, 2013 21:32
-
-
Save rafaellima/5391441 to your computer and use it in GitHub Desktop.
Avoiding switch - case statements
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
rules = Rules.find :all | |
#Calling a method based on it's rule | |
rules.each do |rule| | |
case rule | |
when "foo" | |
foo() | |
when "bar" | |
bar() | |
when "duck" | |
duck() | |
else | |
"fail" | |
end | |
end | |
# AVOIDING CASE | |
rules.each do |rule| | |
Class.send(rule) if Class.respond_to? rule | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment