# Default arguments problem

**URL:** <https://www.ruby-forum.com/t/default-arguments-problem/192746>\
**Category:** Ruby\
**Created:** [August 4, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/default-arguments-problem/192746 "2010-08-04T22:13:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/default-arguments-problem/192746/1 "2010-08-04T22:13:47Z")

</div>

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 )

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:43pm UTC](https://www.ruby-forum.com/t/default-arguments-problem/192746/2 "2010-08-04T22:43:23Z")

</div>

On 2010-08-04 21:13:49 +0100, Amir E. 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

---

<div class="post-metadata">

**Author:** ![amiref](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/41988e/32.png) [@amiref](https://www.ruby-forum.com/u/amiref)\
**Post date:** [August 4, 2010, 10:48pm UTC](https://www.ruby-forum.com/t/default-arguments-problem/192746/3 "2010-08-04T22:48:04Z")

</div>

Amir E. wrote:

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