From: Robert Klemme Date: 2009-08-20T19:57:06+09:00 Subject: Re: #has_arguments? 2009/8/19 Intransition : > Messing with optional argument check for the umpteenth time, eg. > >  def meth(a=Exception) >    if a != Exception >       ... >    else >      ... >    end >  end My 0.02 EUR: if your method's behavior changes depending on argument or argument types chances are that you may rather want different methods. As a side note: I see this sometimes def m(a = nil) @a = a.nil? ? 123 : a end which is of course silly. The same would be much better expressed as def m(a = 123) @a = a end There is no need for additional optional logic - at least in these simple cases. > Other's might do: > >  def meth(*a) >    if !a.empty? >      a = a.first >       ... >    else >      ... >    end >  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... > >  def meth(a=default) >    if has_arguments? >       ... >    else >      ... >    end >  end > > Ah... now that would be nice. It may be useful in some cases but since you can always do the "trick" with "*" I am not sure whether we really need this. I always found block_given? a bit awkward since it looks like a method of self but is rather a quick way to get at information about the argument list. The only reason I use it at times is the fact that the combination m(&b) b.call is slower than the combination m() yield. I'm undecided really. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/