On 4/20/06, Nathan Olberding <nathan.olberding / gmail.com> wrote:
> (Warning: Newbie Altert!)
>
> How do I write a method with optional params? I would think that this:
>
> def do_this(opt1="", opt2="")
>         puts opt2
> end
> do_this(opt2="This")
>
>
> Would result in "This" being displayed. Instead, "This" becomes the
> value of opt1. I'd like to set opt1 and opt2 to default to "". How do I
> do that?

Nathan, looks like you're expecting Ruby to be like Python, but Ruby
doesn't have keyword args -- it just looks at the order of the stuff
pass in to a method, and assigns them to the method parameters in that
order.

In the method call you show above (do_this(opt2="This")), what's
happening (I think) is that some local variable named opt2 (local to
the arg list?) is being created in your argument list, and then the
string "This" is being assigned to it. Then, after that business is
done being evaluated, that string gets passed into the method where
it's assigned to parameter opt1.