On Tue, Aug 18, 2009 at 11:37 PM, Intransition<transfire / gmail.com> wrote:
> Messing with optional argument check for the umpteenth time, eg.
>
> =A0def meth(a=3DException)
> =A0 =A0if a !=3D Exception
> =A0 =A0 =A0 ...
> =A0 =A0else
> =A0 =A0 =A0...
> =A0 =A0end
> =A0end
>
> Other's might do:
>
> =A0def meth(*a)
> =A0 =A0if !a.empty?
> =A0 =A0 =A0a =3D a.first
> =A0 =A0 =A0 ...
> =A0 =A0else
> =A0 =A0 =A0...
> =A0 =A0end
> =A0end
>
> 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...
>
> =A0def meth(a=3Ddefault)
> =A0 =A0if has_arguments?
> =A0 =A0 =A0 ...
> =A0 =A0else
> =A0 =A0 =A0...
> =A0 =A0end
> =A0end
>
> 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 =3D Object.new

   def meth(a=3DMissingArgument)
       if a =3D=3D MissingArgument
       else
       end
   end
end




--=20
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