Named/positional method args
- From: darren kirby <bulliver@xxxxxxxxxxxxxxx>
- Date: Sun, 24 Jun 2007 06:28:23 +0900
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
.
- Follow-Ups:
- Re: Named/positional method args
- From: darren kirby
- Re: Named/positional method args
- From: Gregory Brown
- Re: Named/positional method args
- From: dblack
- Re: Named/positional method args
- From: Tim Hunter
- Re: Named/positional method args
- Prev by Date: Secondary user groups
- Next by Date: Re: Named/positional method args
- Previous by thread: Secondary user groups
- Next by thread: Re: Named/positional method args
- Index(es):
Relevant Pages
|