# Optional arguments and default values

**URL:** <https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449>\
**Category:** Ruby\
**Created:** [December 31, 2005, 12:51pm UTC](https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449 "2005-12-31T12:51:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [December 31, 2005, 12:51pm UTC](https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449/1 "2005-12-31T12:51:31Z")

</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:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449/2 "2006-01-01T21:32:04Z")

</div>

Ross B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> end
> 
> or:
> 
> def foo(\*args)  
> if args.empty?  
> “qwerty”  
> else  
> args.inject(0) { |s,i| s \* i }  
> end  
> end

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

def foo(_args)  
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  
def sum() inject(0) {|a,b| a+b} end  
def product() inject(1) {|a,b| a\*b} end  
end

or

module Enumerable  
def sum() inject {|a,b| a+b} end  
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

```
robert

```

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449/3 "2006-01-01T21:32:01Z")

</div>

On Fri, 30 Dec 2005 19:59:05 -0000, Surgeon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> foo(2,3,5) ----\> 30  
> foo(2,3,5,2) → 60
> 
> foo() -----------\> “qwerty”

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

or:

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

```

Default values are slightly different:

```
def sum(v1 = 10, v2 = 5)
  v1 + v2
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

```
def foo(arg = somemethod('c')) ... end

```

even another arg

```
def foo(a1, a2 = a1) ... end

```

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

Cheers,

---

<div class="post-metadata">

**Author:** ![Surgeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@Surgeon](https://www.ruby-forum.com/u/Surgeon)\
**Post date:** [January 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/optional-arguments-and-default-values/51449/4 "2006-01-01T21:32:08Z")

</div>

On Sat, 31 Dec 2005 12:58:14 -0000, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > "qwerty"
> > 
> > ```
> 
> end  
> Note: if args is empty this inject returns nil, if there is just one  
> element that is returned.

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

Cheers,
