From: "Matthew K. Williams" Date: 2009-08-20T05:47:49+09:00 Subject: Re: #has_arguments? On Thu, 20 Aug 2009, Joel VanderWerf wrote: > Matthew K. Williams wrote: >> As a followup, has_arguments? could be written as: >> >> def has_arguments?(&b) >> vars = eval("local_variables",b.binding) >> return false if vars.length == 0 >> vars.each do |v| >> return false if eval("#{v}.nil?",b) >> end >> return true >> end >> end >> >> >> then you'd call it like: >> >> if has_arguments? {} #note the empty block >> something >> end >> >> >> Matt > > Using #local_variables won't work for this, will it? > > def has_arguments?(&b) > vars = eval("local_variables",b.binding) > return false if vars.length == 0 > vars.each do |v| > return false if eval("#{v}.nil?",b) > end > return true > end > > def foo(*a) > if has_arguments? {} #note the empty block > p a > else > puts "no args" > end > x=3 # <-- N.B. > end > > foo() > foo(1,2,3) > > __END__ > > Output: > > no args > no args > Good point.... I didn't think of that case -- can you suggest a solution?