mardi 27 janvier 2015

Different actions depending on passed arguments: best pattern to choose

I find myself lot of times having this situation, where you want a method to do one thing or other depending what do you pass as arguments. Let mi give an example:


Given this class:



class SendMailJob

def initialize(action_to_perform, user)
self.send(action_to_perform.to_s, args)
end

private
def feedback(user)
puts "We send feedback email #{user}"
end

def any_help(user)
puts "We send any_help email #{user}"
end
end


Then, depending on what I want to do, I call it passing a symbol with the "option":


SendMailJob.new(:feedback, @user)


Is there a better way to do this?


Aucun commentaire:

Enregistrer un commentaire