On Oct 14, 12:11=A0pm, Nikolai Weibull <n... / bitwi.se> wrote:
> On Fri, Oct 14, 2011 at 17:31, Jens Wille <jens.wi... / uni-koeln.de> wrote=
:
> > class A
>
> > =A0def b(c =3D d =3D true) =A0# or some other truthy value
> > =A0 =A0if d
> > =A0 =A0 =A0puts 'no argument was given'
> > =A0 =A0else
> > =A0 =A0 =A0puts c
> > =A0 =A0end
> > =A0end
>
> > end
>
> > can't remember where i first saw it, but it works perfectly ;)
>
> Yes, of course! =A0That=92s the trick I saw too; checking if the optional
> argument=92s default value was evaluated.
>
> Here=92s a more general solution that allows you to set d to a value
> independent of the desired default value for the argument:
>
> def m(a =3D (d =3D true; nil))
> =A0 # d will be true if argument a wasn=92t specified
> end
>
> One easily forgets how expressive Ruby can be. =A0Or how easily it is to =
abuse it.
>
> Thanks!

Well shit, that might be my new favorite too.

  def m(a=3D(na=3Dtrue))
    p a, na
  end

  > m
  true
  true

  > m('foo')
  "foo"
  nil

So, is there a downside to this?