Why is splat argument in ruby not used all the time? -
i know splat arguments used when not know number of arguments passed. wanted know whether should use splat time. there risks in using splat argument whenever pass on arguments?
the splat great when method writing has genuine need have arbitrary number of arguments, method such hash#values_at
.
in general though, if method requires fixed number of arguments it's lot clearer have named arguments pass arrays around , having remember position serves purpose. example:
def file.rename(old_name, new_name) ... end
is clearer than:
def file.rename(*names) ... end
you'd have read documentation know whether old name first or second. inside method, file.rename
need implement error handling around whether had passed correct number of arguments. unless need splat, "normal" arguments clearer.
keyword arguments (new in ruby 2.0) can clearer @ point of usage, although use in standard library not yet widespread.
Comments
Post a Comment