From: darren kirby Date: 2006-08-21T13:17:40+09:00 Subject: Re: [QUIZ] [Solution] QAPrototype (#91) --nextPart2997014.aKLpvyIufQ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Here's solution 2... It now handles args, though somewhat poorly. If you know what you are doing= =20 you can get around this but it will win no points for user-friendlyness. Al= so=20 got rid of the globals, I had to put @imethods within 'method_missing'=20 itself, whether or not I used :attr_accessor I was wrong about not printing one-liners, as I have found [1..array.size-2= ]=20 will return the middle item of a three item array, so it works just fine.=20 Ruby is smart(er than me)... Here is irb session showing usage: ############################ > load 'quiz91.rb' > class Test; include MethodMaker; end > test =3D Test.new =3D> # > test.print_address(:number, :street) No such method: print_address Care to define print_address? (y/n) y Enter method definition ([ctrl-d] when done): puts "#{number} #{street}" =3D> nil irb> test.print_address("Four", "Pennsylvania Plaza") =46our Pennsylvania Plaza =3D> nil > test.print_address(350, "Fifth Avenue") 350 Fifth Avenue =3D> nil > test.print_name("Joe", "Ruby") No such method: print_name=20 Care to define print_name? (y/n) n # bail out, or else 'Joe' and 'Ruby' will be the arg names=20 NoMethodError: print_name from ./quiz91.rb:14:in `method_missing' from (irb):10 > test.print_name(:first,:last) No such method: print_name Care to define print_name? (y/n) y Enter method definition ([ctrl-d] when done): puts "#{first} #{last}" =3D> nil > test.print_name("Joe","Ruby") Joe Ruby =2E.. Code: module MethodMaker def method_missing(method_name, *args) @imethods =3D {} 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.gets.chomp =3D=3D "y" # 'y' prompt_method(method_name, args) else raise NoMethodError, "#{method_name}" end end def prompt_method(name, args=3Dnil)=20 puts "Enter method definition ([ctrl-d] when done):" meth =3D "def #{name}(#{args ? args.join(", ") : ""})" 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 --nextPart2997014.aKLpvyIufQ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBE6TPWwPD5Cr/3CJgRAlMxAKCAkGDrZxZY0RSq+aRrczMwiZJG+gCgvoaM +elIYk5Nuutv8zqHG9bUFio= =Tfkz -----END PGP SIGNATURE----- --nextPart2997014.aKLpvyIufQ--