Using splat (i.e. *some_array ) will work in the general case:
def ensure_file(path, contents, permissions=nil)
In this case, you already get permissions as a parameter, so you can simplify this (and make it even more general) by resolving any number of optional arguments and passing them:
def ensure_file(path, contents, *extra_args) File.open(path, 'w', *extra_args) do |f| f.puts(contents) end end
The only difference is that if too many arguments are passed, an ArgumentError will be raised when File.open called instead of ensure_file .
source share