Defining Optional Argument

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

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 for details. Berlin dates coming soon!

Thanks David, it worked like a charm. :slight_smile:

Grace