I have a method in which i want to pass in dynamic params. The method is called in a loop and sometimes value2 is available and sometimes not.
What is the common way to handle optional method params?
my_method(:value1 => 1,
:value2 => 2 if foo, # this is not working
:value3 => 3)
I usually create a hash like this:
The benefit of this approach is that everyone catches the
if fooas it is a special case. Otherwise many programmers, like myself, will miss this at first glance and get confused why :value2 is not set.Sometimes you have default settings, then you can use this approach:
Or even better: