From: Intransition Date: 2011-10-15T01:18:04+09:00 Subject: Re: What’s the best way of checking if an argument has been given or not? On Oct 14, 12:11 pm, Nikolai Weibull wrote: > On Fri, Oct 14, 2011 at 17:31, Jens Wille wrote: > > class A > > >  def b(c = d = true)  # or some other truthy value > >    if d > >      puts 'no argument was given' > >    else > >      puts c > >    end > >  end > > > end > > > can't remember where i first saw it, but it works perfectly ;) > > Yes, of course!  That’s the trick I saw too; checking if the optional > argument’s default value was evaluated. > > Here’s 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 = (d = true; nil)) >   # d will be true if argument a wasn’t specified > end > > One easily forgets how expressive Ruby can be.  Or how easily it is to abuse it. > > Thanks! Well shit, that might be my new favorite too. def m(a=(na=true)) p a, na end > m true true > m('foo') "foo" nil So, is there a downside to this?