From: James Edward Gray II Date: 2006-08-21T00:11:13+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--461673329 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed Begin forwarded message: > From: "Mario Steele" > Date: August 19, 2006 6:40:57 AM CDT > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Ruby Quiz #91 Solution: > > I decided that when I was bored, I'd do this Quiz, and figured, since > I started on it about 5:00 AM and got done around 6:00 AM CST (With > Taking Breaks and such), that I'd pass this along now. I don't > subscribe to the Ruby Mailing List, but figured if I threw this your > way, you'd be interested in it. I belive I met all the requirements > for this quiz solution, and exceeded a couple of them. Just me being > bored, and deciding that I wanted to get away from my own project, to > relax a bit on a bit of a challenge. > > Read it over, enjoy, and have some fun. ^.^ > > Mario Steele --Apple-Mail-1--461673329 Content-Transfer-Encoding: quoted-printable Content-Type: application/octet-stream; x-unix-mode=0666; name=QAPrototype.rb Content-Disposition: attachment; filename=QAPrototype.rb ##=20RubyQuiz=20#91=20-=20QAPrototype=20by=20Caleb=20Tennis=0D=0A##=20= Your=20Quiz:=20Write=20a=20Ruby=20module=20that=20can=20be=20mixed=20= into=20a=20class.=0D=0A##=20The=20module=20does=20the=20following:=20= upon=20receiving=20a=20method=20call=20that=0D=0A##=20is=20unknown=20to=20= the=20class,=20have=20Ruby=20inform=20the=20user=20that=20it=20doesn't=0D= =0A##=20know=20that=20particular=20method=20call.=20Then,=20have=20it=20= ask=20the=20user=20for=0D=0A##=20Ruby=20code=20as=20input=20to=20use=20= for=20that=20method=20call=20the=20next=20time=20it=20is=0D=0A##=20= called.=0D=0A=0D=0A##=20Example:=0D=0A##=20>=20= object.some_unknown_method=0D=0A=0D=0A##=20some_unknown_method=20is=20= undefined=0D=0A##=20Please=20define=20what=20I=20should=20do=20(end=20= with=20a=20newline):=0D=0A=0D=0A##=20>>=20@foo=20=3D=205=0D=0A##=20>>=20= puts=20"The=20value=20of=20foo=20is=20#{@foo}"=0D=0A##=20>>=0D=0A=0D=0A= ##=20Okay,=20I=20got=20it.=0D=0A=0D=0A##=20>>>=20= object.some_unknown_method=0D=0A##=20"The=20value=20of=20foo=20is=205"=0D= =0A=0D=0A##=20[Editor's=20Note:=0D=0A##=0D=0A##=20I=20envision=20this=20= could=20actually=20be=20handy=20for=20prototyping=20classes=20IRb.=0D=0A= ##=20Bonus=20points=20if=20you=20can=20later=20print=20the=20source=20= for=20all=20methods=20interactively=0D=0A##=20defined.=0D=0A##=0D=0A##=20= --JEG2]=0D=0A=0D=0A##=20Participant:=20=20Mario=20Steele=0D=0A##=20= Email:=20eumario@trilake.net=0D=0A=0D=0Amodule=20QAPrototype=0D=0A=09= ERROR=20=3D=20"!>=20"=0D=0A=09INPUT=20=3D=20"->=20"=0D=0A=09=0D=0A=09def=20= method_missing(meth,*args)=0D=0A=09=09if=20@qaproto_code.nil?=0D=0A=09=09= =09@qaproto_code=20=3D=20{}=0D=0A=09=09end=0D=0A=09=09arg_names=20=3D=20= []=0D=0A=09=09code=20=3D=20""=0D=0A=09=09print=20ERROR,=20"#{meth}=20is=20= undefined\n"=0D=0A=09=09if=20args.length=20!=3D=200=0D=0A=09=09=09print=20= ERROR,=20"Detected=20#{args.length}=20arguments=20given,=20please=20= define=20their=20names=20below"=0D=0A=09=09=09print=20ERROR,=20"Put=20= each=20argument=20name=20on=20a=20seperate=20line=20(end=20with=20= newline)\n\n"=0D=0A=09=09=09loop=20do=0D=0A=09=09=09=09print=20INPUT=0D=0A= =09=09=09=09input=20=3D=20STDIN.gets.strip=0D=0A=09=09=09=09if=20input=20= =3D=3D=20""=0D=0A=09=09=09=09=09if=20arg_names.length=20!=3D=20= args.length=0D=0A=09=09=09=09=09=09print=20ERROR,ERROR,=20"invalid=20= number=20of=20argument=20names!"=0D=0A=09=09=09=09=09else=0D=0A=09=09=09=09= =09=09break=0D=0A=09=09=09=09=09end=0D=0A=09=09=09=09else=0D=0A=09=09=09=09= =09arg_names=20<<=20input=0D=0A=09=09=09=09end=0D=0A=09=09=09end=0D=0A=09= =09end=0D=0A=09=09print=20ERROR,=20"Please=20define=20what=20I=20should=20= do=20(end=20with=20new=20line)\n\n"=0D=0A=09=09loop=20do=0D=0A=09=09=09= print=20INPUT=0D=0A=09=09=09input=20=3D=20STDIN.gets.strip=0D=0A=09=09=09= if=20input=20=3D=3D=20""=0D=0A=09=09=09=09if=20arg_names.length=20>=200=0D= =0A=09=09=09=09=09arg_names=20=3D=20arg_names.join(",")=0D=0A=09=09=09=09= else=0D=0A=09=09=09=09=09arg_names=20=3D=20""=0D=0A=09=09=09=09end=0D=0A=09= =09=09=09code=20=3D=20"def=20#{meth}(#{arg_names})\n"=20+=20code=20+=20= "end"=0D=0A=09=09=09=09self.class.class_eval(code)=0D=0A=09=09=09=09= self.send("qaproto_record_method",*[meth,code])=0D=0A=09=09=09=09print=20= "Okay,=20I=20got=20it.\n"=0D=0A=09=09=09=09break=0D=0A=09=09=09else=0D=0A= =09=09=09=09code=20<<=20input=20+=20"\n"=0D=0A=09=09=09end=0D=0A=09=09= end=0D=0A=09end=0D=0A=09=0D=0A=09def=20qaproto_record_method(meth,code)=0D= =0A=09=09@qaproto_code[meth]=20=3D=20code=0D=0A=09end=0D=0A=09=0D=0A=09= def=20qaproto_display_method(meth)=0D=0A=09=09if=20= @qaproto_code.has_key?(meth)=0D=0A=09=09=09print=20"Ok,=20here=20is=20= the=20code=20for=20#{meth}\n"=0D=0A=09=09=09print=20@qaproto_code[meth]=20= +=20"\n"=0D=0A=09=09else=0D=0A=09=09=09print=20"That=20is=20an=20unknown=20= method=20to=20me.\n"=0D=0A=09=09end=0D=0A=09end=0D=0A=09=0D=0A=09def=20= qaproto_write_method(meth,file)=0D=0A=09=09if=20= @qaproto_code.has_key?(meth)=0D=0A=09=09=09if=20File.exist?(file)=0D=0A=09= =09=09=09print=20"Ok,=20I=20am=20adding=20method=20definition=20#{meth}=20= to=20file=20#{file}\n"=0D=0A=09=09=09=09File.open(file,"a+")=20do=20|fh|=0D= =0A=09=09=09=09=09fh.write(@qaproto_code[meth])=0D=0A=09=09=09=09end=0D=0A= =09=09=09else=0D=0A=09=09=09=09print=20"Ok,=20I=20am=20writting=20method=20= definition=20#{meth}=20to=20file=20#{file}\n"=0D=0A=09=09=09=09= File.open(file,"w")=20do=20|fh|=0D=0A=09=09=09=09=09= fh.write(@qaproto_code[meth])=0D=0A=09=09=09=09end=0D=0A=09=09=09end=0D=0A= =09=09else=0D=0A=09=09=09print=20"That=20is=20an=20unknown=20method=20to=20= me.\n"=0D=0A=09=09end=0D=0A=09end=0D=0A=09=0D=0A=09def=20= qaproto_dump_methods(file)=0D=0A=09=09print=20"Okay,=20I=20am=20writting=20= all=20the=20methods=20that=20I=20have=20learned=20to=20file=20#{file}\n"=0D= =0A=09=09File.open(file,"w")=20do=20|fh|=0D=0A=09=09=09= @qaproto_code.each_value=20do=20|code|=0D=0A=09=09=09=09fh.write(code=20= +=20"\n\n")=0D=0A=09=09=09end=0D=0A=09=09end=0D=0A=09end=0D=0Aend= --Apple-Mail-1--461673329 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1--461673329--