From: James Edward Gray II Date: 2006-08-21T10:15:37+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--425415250 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Begin forwarded message: > From: "Scott Bauer" > Date: August 20, 2006 7:51:13 PM CDT > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > I've attached my submission for Quiz 91 > > Thanks, > > Scott Bauer --Apple-Mail-1--425415250 Content-Transfer-Encoding: quoted-printable Content-Type: application/octet-stream; x-unix-mode=0666; name=quiz91.rb Content-Disposition: attachment; filename=quiz91.rb module=20FastProto=0D=0A=09#=20get=20personal...=0D=0A=09begin=0D=0A=09=09= $NAME=20=3D=20ENV['USER']=09=09=0D=0A=09=09$NAME=20||=3D=20= ENV['HOME'].split("\\").last=0D=0A=09=09$NAME=20||=3D=20`whoami`=0D=0A=09= rescue=0D=0A=09end=09=09=0D=0A=0D=0A=09#=20ahhh,=20yes,=20the=20magical=20= method_missing=0D=0A=09def=20method_missing(sym,=20*args)=0D=0A=09=09= @code_cache=20||=3D=20{}=09=09=0D=0A=0D=0A=09=09#=20to=20get=20the=20= source=20for=20a=20method=20you=20added,=20use=20= instance.source_for=0D=0A=09=09fp_print_source($1.intern)=20= and=20return=20if=20sym.to_s=20=3D~=20/source\_for\_(.*)/=09=09=0D=0A=09=09= =0D=0A=09=09#=20get=20the=20argument=20names=0D=0A=09=09argument_names=20= =3D=20fp_get_argument_names(sym,=20args.size)=0D=0A=0D=0A=09=09#=20get=20= the=20method=20body=0D=0A=09=09code=20=3D=20fp_get_code(sym)=0D=0A=0D=0A=09= =09unless=20code.strip.size.zero?=0D=0A=09=09=09fp_add_method(sym,=20= argument_names,=20code.gsub(/^.*\n/)=20{=20|m|=20"=20=20"=20+=20m=20})=0D= =0A=09=09else=0D=0A=09=09=09puts=20"#{$NAME},=20you=20didnt=20write=20= any=20code,=20how=20could=20you!"=0D=0A=09=09end=0D=0A=09end=0D=0A=0D=0A=09= #=20asks=20nicely=20for=20a=20list=20of=20argument=20names=0D=0A=09def=20= fp_get_argument_names(method_name,=20how_many)=0D=0A=09=09argument_names=20= =3D=20""=0D=0A=09=09#=20if=20arguments=20were=20passed,=20ask=20for=20= their=20names=0D=0A=09=09unless=20how_many.zero?=0D=0A=09=09=09STDOUT=20= <<=20<<-PROMPT=0D=0A#{$NAME},=20I=20noticed=20that=20you=20passed=20a=20= few=20arguments=20to=20#{self.class.name}.#{method_name},=20what=20would=20= you=0D=0Alike=20them=20to=20be=20called?=09Please=20separate=20argument=20= names=20with=20a=20comma=0D=0Asuch=20as=20this:=20name,=20address,=20= phone\n=0D=0A=09=09=09PROMPT=0D=0A=0D=0A=09=09=09while=20= argument_names.to_s.split(',').size=20!=3D=20how_many=0D=0A=09=09=09=09= argument_names=20=3D=20gets.strip=0D=0A=09=09=09=09argument_count=20=3D=20= argument_names.split(',').size=0D=0A=09=09=09=09puts=20"Sorry=20= #{$NAME},=20both=20you=20and=20I=20know=20what=20you=20did,=20now=20fix=20= it=20and=20we=20can=20put=20it\nbehind=20us."=20if=20argument_count=20!=3D= =20how_many.size=0D=0A=09=09=09end=0D=0A=09=09end=09=09=0D=0A=09=09= argument_names=0D=0A=09end=0D=0A=0D=0A=09#=20gets=20the=20ruby=20code=20= for=20the=20new=20method=20from=20the=20user=0D=0A=09def=20= fp_get_code(method_name)=0D=0A=09=09puts=20"#{$NAME},=20please=20enter=20= the=20code=20for=20#{self.class.name}.#{method_name}=20(end=20with=20a=20= newline)\n"=0D=0A=0D=0A=09=09#=20read=20lines=20until=20a=20single=20= newline=20is=20keyed=0D=0A=09=09code=20=3D=20""=0D=0A=09=09until=20= (STDOUT=20<<=20"#>=20";=20code_line=20=3D=20gets)=20=3D=3D=20$/=0D=0A=09=09= =09code=20<<=20code_line=0D=0A=09=09end=0D=0A=09end=0D=0A=0D=0A=09#=20= prints=20the=20source=20for=20a=20dynamically=20created=20method=0D=0A=09= def=20fp_print_source(method_name)=0D=0A=09=09if=20= @code_cache.include?(method_name)=0D=0A=09=09=09puts=20"Why=20#{$NAME},=20= the=20source=20for=20#{self.class.name}.#{method_name}=20just=20happens=20= to=20be:\n#{@code_cache[method_name]}"=0D=0A=09=09else=0D=0A=09=09=09= puts=20"Sorry=20#{$NAME},=20you=20havent=20defined=20that=20method=20= yet"=0D=0A=09=09end=0D=0A=09=09true=0D=0A=09end=0D=0A=0D=0A=09#=20adds=20= the=20new=20method=20to=20the=20code=20cache=20and=20adds=20the=20method=20= to=20the=20current=20instances=20class=0D=0A=09def=20fp_add_method(sym,=20= argument_names,=20code)=0D=0A=09=09@code_cache[sym]=20=3D=20"#mixed=20= into=20#{self.class.name}\ndef=20= #{sym}(#{argument_names})\n#{code.rstrip}\nend"=0D=0A=09=09= self.class.class_eval(@code_cache[sym])=0D=0A=09end=0D=0Aend=0D=0A= --Apple-Mail-1--425415250 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1--425415250--