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 1. any suggestions on what I should do here? Thanks in advance. Grace -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Sat, 8 Mar 2008, Grace Xue wrote:> > 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?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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks David, it worked like a charm. :) Grace -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---