From: Amir Ebrahimifard on
Hi
How can I have a method with 3 arguments so that only second argument is
a default
argument for example:

def sum(p,q=2,r)
puts p+q+r
end

(I want to use from this method with only 2 arguments : first argument
and third argument )
--
Posted via http://www.ruby-forum.com/.

From: Jason Watson on
On 2010-08-04 21:13:49 +0100, Amir Ebrahimifard said:

> def sum(p,q=2,r)
> puts p+q+r
> end

Does the default arg have to be the second one? If not just move it to
the end of the args list.

def sum(p,r, q=2)
puts p+q+r
end





From: Joel VanderWerf on
Amir Ebrahimifard wrote:
> Hi
> How can I have a method with 3 arguments so that only second argument is
> a default
> argument for example:
>
> def sum(p,q=2,r)
> puts p+q+r
> end
>
> (I want to use from this method with only 2 arguments : first argument
> and third argument )

Only with ruby 1.9:

$ irb19
>> def sum(p,q=2,r)
>> puts p+q+r
>> end
=> nil
>> sum(1,2,3)
6
=> nil
>> sum(1,3)
6
=> nil
>>


 | 
Pages: 1
Prev: ruby watir on Win2008R2
Next: Return value problem