Skip to content

Instantly share code, notes, and snippets.

@rafaellima
Created April 15, 2013 21:32
Show Gist options
  • Save rafaellima/5391441 to your computer and use it in GitHub Desktop.
Save rafaellima/5391441 to your computer and use it in GitHub Desktop.
Avoiding switch - case statements
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