From: "Matthew K. Williams" Date: 2009-08-20T02:39:04+09:00 Subject: Re: #has_arguments? --8323329-1090091473-1250703685=:28729 Content-Type: MULTIPART/MIXED; BOUNDARY="8323329-1090091473-1250703685=:28729" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-1090091473-1250703685=:28729 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Thu, 20 Aug 2009, Trans wrote: > > > On Aug 19, 7:53=A0am, Roger Pack wrote: >>> =A0def meth(*a) >>> =A0 =A0if !a.empty? >>> =A0 =A0 =A0a =3D a.first >>> =A0 =A0 =A0 ... >>> =A0 =A0else >>> =A0 =A0 =A0... >>> =A0 =A0end >>> =A0end >> >> Maybe morph this to >> >> def meth(*a) >> =A0 if has_arguments? a >> =A0 =A0 ... >> =A0 else >> =A0 =A0 ... >> =A0 end >> end > > Either way is fine. The defined method interface doesn't matter. > #has_arguments? would simply report false if a call to the method > passed no arguments. If the interface definition itself does not allow > for no arguments, eg. 'def meth(a)', then #has_arguments? would simply > never return false. > > T. > > I'm still not convinced this is necessarily needed, but, how about this=20 for a first stab (still looking at whether there's a way to not need the=20 explicit call to binding) : class Object def has_arguments?(b) vars =3D eval("local_variables",b) return false if vars.length =3D=3D 0 vars.each do |v| return false if eval("#{v}.nil?",b) end return true end end class Foo def initialize(first=3Dnil) if (has_arguments? binding) puts "We're defined" else puts "Not!" end end def no_args if (has_arguments? binding) puts "We're defined" else puts "Not!" end end def mandatory_arg(a) if (has_arguments? binding) puts "We're defined" else puts "Not!" end end end Matt --8323329-1090091473-1250703685=:28729-- --8323329-1090091473-1250703685=:28729--