From: Rick DeNatale Date: 2009-08-19T22:09:45+09:00 Subject: Re: #has_arguments? On Tue, Aug 18, 2009 at 11:37 PM, Intransition wrote: > Messing with optional argument check for the umpteenth time, eg. > >  def meth(a=Exception) >    if a != Exception >       ... >    else >      ... >    end >  end > > Other's might do: > >  def meth(*a) >    if !a.empty? >      a = a.first >       ... >    else >      ... >    end >  end > > Neither of which are very satisfying. So it occurs to me tonight that > we already have #has_block? to see if a block was passed. So how about > a #has_arguments? to query if _any_ arguments have been passed. So > then... > >  def meth(a=default) >    if has_arguments? >       ... >    else >      ... >    end >  end > > Ah... now that would be nice. I've never felt the need for this. Normally I just make the default value for an optional parameter nil, and check for nil. If nil is a valid value, then I'd do something like class A MissingArgument = Object.new def meth(a=MissingArgument) if a == MissingArgument else end end end -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale