I am trying to pass default parameters in the method but not able to do so till now.I have written a method with below signature.
def abc(a,b=22,c,d=55)
end
i am getting error for above code as "syntax error, unexpected '=', expecting ')'".
If i replace the above code with the code shown below then it works fine.
def abc(a,b=5,c)
end
what could be the reason for this??
Thanks
The problem, I think, is what happens when you pass different numbers of arguments.
abc(1,2,3,4)
a=1, b=2, c=3, d=4
Pretty clear what the assignment should be.
But, how could you possibly fail to set the second parameter?
Is this missing the second, or the fourth parameter? What gets the default?
abc(m, t, z)
How could I miss out the second parameter, and leave the third with a useful value - a parameter that doesn't get a default?
You should group the defaulting parameters together:
def abc(a=25, b=6, c, d)
or:
def abc(a, b, c=6, d=7)
and then the behaviour is more predictable.
You probably should be looking at an options hash, though. A much more flexible way to pass variable arguments and some neat methods to handle missing arguments.