From: JB Eriksson Date: 2005-10-24T00:40:06+09:00 Subject: Re: [QUIZ] Ducksay (#52) ------=_Part_21731_7601876.1130082002140 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline my solution. not very OO at all, however. I just hope the formatting doesn'= t screw up on the animal template files... /Johan B Eriksson ----------- cowsay.rb: ----------- # Rubyquiz #52, Ducksay # ascii animals talking using ascii comic balloons. # # # I couldn't get commandline arguments working properly on my machine, so I skipped it. # Instead, change the variable values to suit your needs. # # This solution does not automatically wordwrap the sayings. Do it manually= . #templates are text files with ascii character and "#tail#" for #the tail character, "#eye#" for one eye and "#eyes#" for two eyes. #example templates are cow.txt, duck.txt and duck2.txt template =3D "duck2.txt" #edit saying as per your wishes saying =3D "Fourscore and seven years ago our fathers brought\nforth on thi= s continent a new nation,\nconceived in liberty and dedicated to the\nproposition that all men are created equal." #edit eye-style, "eyes" is for templates with two eyes, "eye" for one-eyed templates eyes =3D "oo" eye =3D "o" #tongue, two chars tongue =3D "@ " #"think" for think-cloud, atm, there's only think and the default ("say") a= s per cowsay.net cloud_type =3D "say" #If you want another style of cloud, add one here. #Also, the clouds tail-style is decided here as well. #Remember to escape special characters. if cloud_type =3D=3D "think" then cloud_oneline_left =3D "(" cloud_oneline_right =3D ")" cloud_upper_left =3D "(" cloud_upper_right =3D ")" cloud_middle_left =3D "(" cloud_middle_right =3D ")" cloud_lower_left =3D "(" cloud_lower_right =3D ")" tail =3D "o" else cloud_oneline_left =3D "<" cloud_oneline_right =3D ">" cloud_upper_left =3D "/" cloud_upper_right =3D "\\" cloud_middle_left =3D "|" cloud_middle_right =3D "|" cloud_lower_left =3D "\\" cloud_lower_right =3D "/" tail =3D "\\" end #The ducksay application #prints out as many spaces as the first argument (an integer), #then puts the string, which defaults to "" def output_spaces (intnumber, string=3D"") intnumber.times { print " " } puts string end def output_chars (intnumber, string=3D" ") intnumber.times { print string } puts end #the balloon, and the saying print " " #put start of the balloon at proper place unless saying.include? ?\n then #single line saying output_chars(saying.length, "_") puts " " + cloud_oneline_left + saying + cloud_oneline_right print " " #put closing of balloon at proper place output_chars(saying.length, "-") puts else #multiple line saying saying =3D saying.split("\n") longest_line =3D 0 saying.each do |x| longest_line =3D x.length if (x.length > longest_line) end output_chars(longest_line, "_") #First line of the multi-line cloud current_line =3D saying.shift print_spaces =3D longest_line - current_line.length print " " + cloud_upper_left + current_line output_spaces(print_spaces, cloud_upper_right) #The rest of the lines while not saying.empty? current_line =3D saying.shift print_spaces =3D longest_line - current_line.length if saying.empty? then #When on the last line, "close" the cloud with cloud_lower_left and _right chars print " " + cloud_lower_left + current_line output_spaces(print_spaces, cloud_lower_right) else #Put proper left and right chars for the cloud when in the "middle" of it print " " + cloud_middle_left + current_line output_spaces(print_spaces, cloud_middle_right) end end print " " # put the clouds closing at proper place output_chars(longest_line, "-") end #print out the template, aka, the ascii animal #in the template files, "=C5" substitutes the tail, "=C4" one eye, and "=C4= =C4" two eyes File.open(template) do |file| while line =3D file.gets #substitute the placeholder characters with the cloud tail and eye(s), then print it out line.gsub!("#tail#", tail) line.gsub!("#eyes#", eyes) line.gsub!("#eye#", eye) line.gsub!("#tongue#", tongue) puts line end end ----------- cow.txt: ----------- #tail# ^__^ #tail# (#eyes#)\_______ (__)\ )\/\ #tongue# ||----w | || || ----------- duck.txt: ----------- #tail# #tail# .~~. '_-( #eyes# ) '-'=3D=3D(___/) ( ( . / \ '- ' / ----------- duck2.txt: ----------- #tail# ` >(#eye#)____, (` =3D~~/ ^~^~^~^~^~`---'^~^~^~^~^~ ----------- ------=_Part_21731_7601876.1130082002140--