From: darren kirby Date: 2006-08-20T21:34:07+09:00 Subject: Re: [QUIZ] [Solution] QAPrototype (#91) --nextPart6413066.WPbtAHqSN2 Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Well, here is my solution. I kind of surprised myself at how short it is, b= ut=20 then, there are many ways I could make this much better. The biggest proble= m=20 is that it cannot deal with arguments to the methods. It uses a global to=20 keep track of the interactively coded methods, I just couldn't get it to wo= rk=20 any other way. I guess this is because modules/mixins are not supposed to=20 have instance variables or keep track of their own state. The print_method= =20 function will break if your method is a one-liner. I know I could do some=20 case-analysis to take care of this but I am a lazy, lazy man. Here is a brief irb session demonstrating it, the code follows... ######################################### > load 'quiz91.rb' > class Test; include MethodMaker; end > test =3D Test.new > test.foobar() No such method: foobar Care to define foobar? (y/n) y Enter method definition ([ctrl-d] when done): first =3D "Ruby" middle =3D "is teh" last =3D "roXX0r" puts "#{first} #{middle} #{last}" =3D> nil > test.foobar() Ruby is teh roXX0r =3D> nil > test.print_method("foobar") def foobar first =3D "Ruby" middle =3D "is teh" last =3D "roXX0r" puts "#{first} #{middle} #{last}" end =3D> nil > test.another_one() No such method: another_one Care to define another_one? (y/n) y Enter method definition ([ctrl-d] when done): a =3D 4 b =3D 5 c =3D 67 return a + b + c =3D> nil > x =3D test.another_one() =3D> 76 > x =3D> 76 > test.print_method("another_one") def another_one a =3D 4 b =3D 5 c =3D 67 return a + b + c end > ... ######################################### module MethodMaker $imethods =3D Hash.new def method_missing(method_name) puts "No such method: #{method_name}" # It might be a simple typo... # so give a chance to bail out. print "Care to define #{method_name}? (y/n) " if $stdin.getc =3D=3D 121 # 'y' prompt_method(method_name) else raise NoMethodError, "#{method_name}" end end def prompt_method(name)=20 puts "Enter method definition ([ctrl-d] when done):" meth =3D "def #{name}" while $stdin.gets meth +=3D $_ end meth +=3D "end" meth =3D meth.gsub("\n",";") $imethods["#{name}"] =3D meth eval meth end def print_method(name) meth_Array =3D $imethods[name].split(";") puts meth_Array[0] meth_Array[1..meth_Array.size-2].each { |line| puts " #{line}" } puts meth_Array[-1] end end ######################################## =2Dd =2D-=20 darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." =2D Dennis Ritchie and Ken Thompson, June 1972 --nextPart6413066.WPbtAHqSN2 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBE6FavwPD5Cr/3CJgRAkW/AJ9uaPau/U/4fWBwe3d06kEc75ldZwCgohkh Tot1mYMk6/pvq2K2+QkeYi0= =yUz5 -----END PGP SIGNATURE----- --nextPart6413066.WPbtAHqSN2--