# Defining Optional Argument

**URL:** <https://www.ruby-forum.com/t/defining-optional-argument/131323>\
**Category:** Rails\
**Created:** [March 8, 2008, 3:04am UTC](https://www.ruby-forum.com/t/defining-optional-argument/131323 "2008-03-08T03:04:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e495f1/32.png) [@wings](https://www.ruby-forum.com/u/wings)\
**Post date:** [March 8, 2008, 3:04am UTC](https://www.ruby-forum.com/t/defining-optional-argument/131323/1 "2008-03-08T03:04:10Z")

</div>

Hi all.

I’m trying to create an optional argument that if it’s not passed, a  
default value will be assigned.

def graph (graph\_type, graph\_period, \*hidden)

hidden = 1 if hidden.nil

end

1. hidden is a tinyint (0 or 1), \* is used for hash, so what’s the best  
syntex that I should have here?

2. hidden.nil doesn’t seem to work, as it’s not assigning the hidden =

3. any suggestions on what I should do here?

Thanks in advance.

Grace

---

<div class="post-metadata">

**Author:** ![wings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e495f1/32.png) [@wings](https://www.ruby-forum.com/u/wings)\
**Post date:** [March 8, 2008, 3:15am UTC](https://www.ruby-forum.com/t/defining-optional-argument/131323/2 "2008-03-08T03:15:54Z")

</div>

Hi –

On Sat, 8 Mar 2008, Grace X. wrote:

> end
> 
> 1. hidden is a tinyint (0 or 1), \* is used for hash, so what’s the best  
> syntex that I should have here?

What you want is:

def graph(graph\_type, graph\_period, hidden=1)

(\*hidden will set hidden to an array (not a hash), and that’s why it’s  
never nil.)

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS, April 14-17 2008, New York City  
CORE RAILS, June 24-27 2008, London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details. Berlin dates coming soon!

---

<div class="post-metadata">

**Author:** ![wings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e495f1/32.png) [@wings](https://www.ruby-forum.com/u/wings)\
**Post date:** [March 8, 2008, 3:23am UTC](https://www.ruby-forum.com/t/defining-optional-argument/131323/3 "2008-03-08T03:23:07Z")

</div>

Thanks David, it worked like a charm. 🙂

Grace
