On Oct 14, 11:31=A0am, Jens Wille <jens.wi... / uni-koeln.de> wrote:
> luke gruber [2011-10-14 17:15]:> That way, the instance won't be availabl=
e across classes and you
> > can just check if the argument responds_to :missing?
>
> or you can just do this:
>
> class A
>
> =A0 def b(c =3D d =3D true) =A0# or some other truthy value
> =A0 =A0 if d
> =A0 =A0 =A0 puts 'no argument was given'
> =A0 =A0 else
> =A0 =A0 =A0 puts c
> =A0 =A0 end
> =A0 end
>
> end
>
> can't remember where i first saw it, but it works perfectly ;)

That's pretty cleaver, although it reads perhaps a bit too strangely.

What I have always done is.... obviously, if `nil` is not a valid
argument, then just use that.

  def b(a=3Dnil)
    if a
      ...
  end

But if `nil` is valid then use `ArgumentError`, as that won't be a
valid argument in 99.99% (or so) cases anyway.

  def b(a=3DArgumentError)
    if a =3D=3D ArgumentError
      ...
  end

For the most robust solution, Ruby Facets defines a toplevel constant
called `NA`, the sole purpose of which is to fill in for cases such as
this.

  def b(a=3DNA)
    if a =3D=3D NA
      ...
  end

That's my favorite, but it require's a dependency on Facets or copy
and pasting the extra code to one's project.

(hmm... it occurs to me that global `$NA` variable would be good to
have too so there's no namespace issues such as in a BasicObjcet
subclass)