From: Trans Date: 2009-08-20T00:08:18+09:00 Subject: Re: #has_arguments? On Aug 19, 9:09 am, Rick DeNatale wrote: > 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 Right, it precisely the times when nil is a valid value that it is a problem. > class A > >     MissingArgument = Object.new > >    def meth(a=MissingArgument) >        if a == MissingArgument >        else >        end >    end > end Hmm... doesn't that indicate a need? ;) And yes, I've done that before too, but it falls into the realm of my first example. Exception is typically just as good as MissingArgument, and that way you don't need a stray object that exists just to exist. But in either case it is still a (albeit minor) jerry-rig. What we have here is a nail, and I'd much rather have a nice hammer than to keep using this damn rock.