From: Bob Lisbonne Date: 2007-04-24T07:12:44+09:00 Subject: Re: [QUIZ] Morse Code (#121) Here is a second, cleaned up version of my posting which benefits from James Edward Gray's fine suggestions. thanks, /Bob #!/usr/bin/env ruby -w CW = {'.-'=>'A','-...'=>'B','-.-.'=>'C','-..'=>'D','.'=>'E','..-.'=>'F','--.' =>'G','....'=>'H','..'=>'I','.---'=>'J','-.-'=>'K','.-..'=>'L','--'=>'M' ,'-.'=>'N','---'=>'O','.--.'=>'P','--.-'=>'Q','.-.'=>'R','...'=>'S','-'= >'T','..-'=>'U','...-'=>'V','.--'=>'W','-..-'=>'X','-.--'=>'Y','--..'=> 'Z'} def morse(dotsanddashes,letters) if dotsanddashes.empty? then puts letters else CW.keys.each do |try| if /^#{Regexp.escape(try)}/.match(dotsanddashes) then morse(dotsanddashes[$&.size,dotsanddashes.size],letters+ CW[$&]) end end end end #morse morse(STDIN.read.chomp,'')