I have an action method that takes a variable number of arguments, like this:
action!(:message => message, :photo => photo, :status => status)
If an argument is included it must be valid (based on certain criteria), so something like this won’t work:
action!(:message => nil, :photo => nil, :status => nil)
With this in mind I want to be able to only include an argument if it is valid. Naively I know I can do it like this:
if message.valid? && photo.valid? && status.valid?
action!(:message => message, :photo => photo, :status => status)
elsif message.valid? && photo.valid?
action!(:message => message, :photo => photo)
...
end
While this works its really ugly and inefficient. Is there a better way to build the right set of arguments for this call?
The arguments are just a hash so why not just create a hash object and add what you need to it: