On Aug 18, 11:37=A0pm, Intransition <transf... / gmail.com> wrote:
> Messing with optional argument check for the umpteenth time, eg.
>
> =A0 def meth(a=3DException)
> =A0 =A0 if a !=3D Exception
> =A0 =A0 =A0 =A0...
> =A0 =A0 else
> =A0 =A0 =A0 ...
> =A0 =A0 end
> =A0 end
>
> Other's might do:
>
> =A0 def meth(*a)
> =A0 =A0 if !a.empty?
> =A0 =A0 =A0 a =3D a.first
> =A0 =A0 =A0 =A0...
> =A0 =A0 else
> =A0 =A0 =A0 ...
> =A0 =A0 end
> =A0 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...
>
> =A0 def meth(a=3Ddefault)
> =A0 =A0 if has_arguments?
> =A0 =A0 =A0 =A0...
> =A0 =A0 else
> =A0 =A0 =A0 ...
> =A0 =A0 end
> =A0 end
>
> Ah... now that would be nice.

Here's a little trick I learned (very hacky):

  def meth(a=3D(no_argument=3D'default'))
    if no_argument
      'nothing given'
    else
      "we got #{a.inspect}"
    end
  end

Your default argument can't be falsy (but you could use something
truey and use your falsy value in its place).  Anyway:

  036:0> meth
  "nothing given"
  037:0> meth(nil)
  "we got nil"
  038:0> meth('whatever')
  "we got "whatever""
  039:0> meth
  "nothing given"

I can't vouch for JRuby or Ruby 1.9 or Rubinius or IronRuby for this
one (work's a strictly 1.8.6 kind of place).

-Chris