Named/positional method args



I have a method here that takes two arguments. Both are optional, with
appropriate default values. The first is a string which represents a filename
to write data to. The second is an integer which represents which data from a
choice of 1 or more should be written to the file.

So I have:

def write_picture(outfile = nil, n = 1)

When I call as:

write_picture()

it does the right thing and uses both defaults.

When I call as:

write_picture("somefile")

it does the right thing and writes the default data to "somefile"

When I call:

write_picture("front cover", 1)
write_picture("back cover", 2)

it does the right thing. However, when I do:

write_picture(n=1)

I am sure you can guess what happens. I get a picture written to a file
with "1" as a filename. This seems like non-intuitive behavior. With no
support for named args shouldn't it error here?

Now: I gather I can do:

def write_picture(*args)

and perhaps do a type check on each argument, as there must be one each String
and Integer, and sort them out appropriately. However, this relies too
heavily, for my comfort, on the users passing sane values to the method.

Is there a clean way to accomplish my goal? Are there plans to give named
arguments to Ruby in the future?

Thanks for consideration,
-d
--
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

.



Relevant Pages

  • Re: Search from asxp page not working
    ... have a question about the query string and finding parts of filename. ... Microsoft Online Community Support ...
    (microsoft.public.inetserver.indexserver)
  • file class
    ... private String _FileName; ... /// filename and all reqired parameters must be set up by user ... public TextFileServer(string FileName, int FixedRecordLen) ... //End of line built from constructor or user manual ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: file class
    ... private String _FileName; ... /// filename and all reqired parameters must be set up by user ... public TextFileServer(string FileName, int FixedRecordLen) ... //End of line built from constructor or user manual ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: file class
    ... private String _FileName; ... /// filename and all reqired parameters must be set up by user ... public TextFileServer(string FileName, int FixedRecordLen) ... //End of line built from constructor or user manual ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: unbuffered file IO?
    ... private String _FileName; ... /// filename and all reqired parameters must be set up by user ... public TextFileServer(string FileName, int FixedRecordLen) ... //End of line built from constructor or user manual ...
    (microsoft.public.dotnet.languages.csharp)