I need to check the presence of multiple params. Currently what i have written is
if params[:p1].present? && params[:p2].present? && params[:p3].present?
# Do something
end
Is there a more efficient way to do this?
You can use the Enumerable.all?
method:
%i( p1 p2 p3 ).all? { |key| params[key].present? }
Another alternative, if you need the values, it to fetch them and check the presence.
params.values_at(*%i( p1 p2 p3 )).all?(&:present?)
or
params.values_at(:p1, :p2, :p3).all?(&:present?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With