# Optional arguments and default values

**URL:** <https://rubytalk.org/t/optional-arguments-and-default-values/23709>\
**Category:** ruby-talk\
**Created:** [31 December 2005 11:09 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709 "2005-12-31T11:09:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mubin\_Aktan](https://avatars.discourse-cdn.com/v4/letter/m/45deac/32.png) [@Mubin\_Aktan](https://rubytalk.org/u/Mubin_Aktan)\
**Post date:** [31 December 2005 11:09 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709/1 "2005-12-31T11:09:32Z")

</div>

Hi,

How do I give a method optional arguments and default values?

Exmpl:

foo is a function that multiplies all of its arguments together. If  
there is not any argument, a default value of "qwerty" returns.

foo(2,3) ----\> 6  
foo(2,3,5) ----\> 30  
foo(2,3,5,2) -\> 60

foo() -----------\> "qwerty"

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [31 December 2005 11:06 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709/2 "2005-12-31T11:06:52Z")

</div>

Maybe:  
&nbsp;&nbsp;def foo(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;args.empty? && "qwerty" or args.inject(0) { |s,i| s \* i }  
&nbsp;&nbsp;end

or:

&nbsp;&nbsp;def foo(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;if args.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"qwerty"  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args.inject(0) { |s,i| s \* i }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Default values are slightly different:

&nbsp;&nbsp;def sum(v1 = 10, v2 = 5)  
&nbsp;&nbsp;&nbsp;&nbsp;v1 + v2  
&nbsp;&nbsp;end

Once you give a default value to an arg, you must also give defaults to all following args (except any &block arg).

Defaults don't have to be literal - you can use anything

&nbsp;&nbsp;def foo(arg = somemethod('c')) ... end

even another arg

&nbsp;&nbsp;def foo(a1, a2 = a1) ... end

I believe they're evaluated in the scope of the method itself. It's quite cool.

Cheers,

> **···**
>
> On Fri, 30 Dec 2005 19:59:05 -0000, Surgeon \<biyokuantum@gmail.com\> wrote:
> 
> > Hi,
> > 
> > How do I give a method optional arguments and default values?
> > 
> > Exmpl:
> > 
> > foo is a function that multiplies all of its arguments together. If  
> > there is not any argument, a default value of "qwerty" returns.
> > 
> > foo(2,3) ----\> 6  
> > foo(2,3,5) ----\> 30  
> > foo(2,3,5,2) -\> 60
> > 
> > foo() -----------\> "qwerty"
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi3](https://avatars.discourse-cdn.com/v4/letter/g/7bcc69/32.png) [@gabriele\_renzi3](https://rubytalk.org/u/gabriele_renzi3)\
**Post date:** [31 December 2005 11:07 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709/3 "2005-12-31T11:07:48Z")

</div>

Surgeon ha scritto:

> Hi,
> 
> How do I give a method optional arguments and default values?

\>\> def f(a=1,b=2)  
\>\> a+b  
\>\> end  
=\> nil  
\>\> f  
=\> 3  
\>\> f 2  
=\> 4  
\>\> f 2,3  
=\> 5

> Exmpl:
> 
> foo is a function that multiplies all of its arguments together. If  
> there is not any argument, a default value of "qwerty" returns.
> 
> foo(2,3) ----\> 6  
> foo(2,3,5) ----\> 30  
> foo(2,3,5,2) -\> 60
> 
> foo() -----------\> "qwerty"

maybe you want to get all the "rest" arguments with the "\*"  
declaration in the method like this:

\>\> def foo(\*args) # args are put in an array named args  
\>\> return "qwerty" if args.empty?  
\>\> args.inject do |acc,elem|  
?\> acc\*elem  
\>\> end  
\>\> end  
=\> nil  
\>\> foo  
=\> "qwerty"  
\>\> foo 1,2,3,4  
=\> 24

But I suggest you take a look at some ruby tutorial, this are basic things.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [31 December 2005 13:02 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709/4 "2005-12-31T13:02:56Z")

</div>

> > Hi,
> > 
> > How do I give a method optional arguments and default values?
> > 
> > Exmpl:
> > 
> > foo is a function that multiplies all of its arguments together. If  
> > there is not any argument, a default value of "qwerty" returns.
> > 
> > foo(2,3) ----\> 6  
> > foo(2,3,5) ----\> 30  
> > foo(2,3,5,2) -\> 60
> > 
> > foo() -----------\> "qwerty"
> 
> Maybe:  
> def foo(\*args)  
> &nbsp;&nbsp;args.empty? && "qwerty" or args.inject(0) { |s,i| s \* i }  
> end
> 
> or:
> 
> def foo(\*args)  
> &nbsp;&nbsp;if args.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;"qwerty"  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;args.inject(0) { |s,i| s \* i }  
> &nbsp;&nbsp;end  
> end

The implementation of Enumerable#inject allows for an even more elegant solution

def foo(\*args)  
&nbsp;&nbsp;args.inject {|a,b| a\*b} || "qwerty"  
end

> > foo 2,3,5

=\> 30

> > foo 2,3

=\> 6

> > foo 2

=\> 2

> > foo

=\> "qwerty"

Note: if args is empty this inject returns nil, if there is just one element that is returned.

Another solution would be to implement things like this in Enumerable:

module Enumerable  
&nbsp;&nbsp;def sum() inject(0) {|a,b| a+b} end  
&nbsp;&nbsp;def product() inject(1) {|a,b| a\*b} end  
end

or

module Enumerable  
&nbsp;&nbsp;def sum() inject {|a,b| a+b} end  
&nbsp;&nbsp;def product() inject {|a,b| a\*b} end  
end

Allowing for invocations like these

> > [1,2,3,4].sum

=\> 10

> > (1..4).sum

=\> 10

> > (1...5).sum

=\> 10

> > (1..10).map { rand 20 }.sum

=\> 91

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Ross Bamford \<rosco@roscopeco.remove.co.uk\> wrote:
> 
> > On Fri, 30 Dec 2005 19:59:05 -0000, Surgeon \<biyokuantum@gmail.com\> \> wrote:

---

<div class="post-metadata">

**Author:** ![Ross\_Bamford2](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Ross\_Bamford2](https://rubytalk.org/u/Ross_Bamford2)\
**Post date:** [31 December 2005 18:17 UTC](https://rubytalk.org/t/optional-arguments-and-default-values/23709/5 "2005-12-31T18:17:55Z")

</div>

Cool, I didn't know that, thanks for pointing it out 🙂

Cheers,

> **···**
>
> On Sat, 31 Dec 2005 12:58:14 -0000, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > Ross Bamford \<rosco@roscopeco.remove.co.uk\> wrote:
> > 
> > > Maybe:  
> > > def foo(\*args)  
> > > &nbsp;&nbsp;args.empty? && "qwerty" or args.inject(0) { |s,i| s \* i }  
> > > end
> > > 
> > > or:
> > > 
> > > def foo(\*args)  
> > > &nbsp;&nbsp;if args.empty?  
> > > &nbsp;&nbsp;&nbsp;&nbsp;"qwerty"  
> > > &nbsp;&nbsp;else  
> > > &nbsp;&nbsp;&nbsp;&nbsp;args.inject(0) { |s,i| s \* i }  
> > > &nbsp;&nbsp;end  
> > > end
> > 
> > The implementation of Enumerable#inject allows for an even more elegant solution
> > 
> > def foo(\*args)  
> > &nbsp;&nbsp;args.inject {|a,b| a\*b} || "qwerty"  
> > end
> > 
> > > > foo 2,3,5
> > 
> > =\> 30
> > 
> > > > foo 2,3
> > 
> > =\> 6
> > 
> > > > foo 2
> > 
> > =\> 2
> > 
> > > > foo
> > 
> > =\> "qwerty"
> > 
> > Note: if args is empty this inject returns nil, if there is just one element that is returned.
> 
> --  
> Ross Bamford - rosco@roscopeco.remove.co.uk
