On 6/23/07, darren kirby <bulliver / badcomputer.org> wrote:

> 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?

No, that just becomes write_picture(1)

the reason for this is that arguments can be expressions, not just
singular values.

> 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?

Yes.  use a hash

def my_method(options={})
  a = options[:a]
  b = options[:b]
end

my_method(:a => 1, :b => 2)
my_method(:b => 3)
my_method(:a => 1)

all work.  You can then do something like

def my_method(options={})
  a = options[:a] || 10
  b = options[:b] || 5
end

to set defaults, or if you have a whole bunch, look at Hash#merge.

> Are there plans to give named arguments to Ruby in the future?

Not that I know of but syntactic sugar will be allowed for hash
definition, which will allow

my_method( :foo => 2, :bar => 10)

to become

my_method( :foo : 2, :bar : 10)

(I think...)