From: Adam Shelly Date: 2005-08-26T06:10:29+09:00 Subject: Re: [SUMMARY] Sodoku Solver (#43) On 8/25/05, Ruby Quiz wrote: > I believe Adam Shelly's parse routine could benefit from similar simplifications > if you want to try your own hand at a little refactoring. Ok, I admit I'm a ruby newbie. But I'm trying to learn the Ruby Way. How's this? Was: def ParseBoard(file) row = 0 col = 0 boxes = 0 boardlist = [ ] file.each do |line| line.chomp.each_byte do |c| case c when ?0..?9 boardlist << c.to_i - ?0 col+=1 when ?A..?Z boardlist << c.to_i - ?A + 10 col+=1 when ?a..?z boardlist << c.to_i - ?a + 10 col+=1 when ?_ boardlist << -1 col+=1 when ?+ boxes+=1 if row == 0 end end if (col > 0) then row+=1 break if (row == col) end col=0 end @@boxcols = boxes-1 return boardlist,row end Is: def ParseBoard(file) boardlist,size = [ ],0 file.each do |line| if line.delete!('^0-9A-Za-z_+') =~ /\++/ @@boxcols= line.length-1 else boardlist += line.split(//).collect{|n| n.hex } size = line.length end p line end boardlist,size end The only difference is it adds 0's to the boardlist where it used to add -1's, but the solver constructor handles anything outside 1..size the same, so it doesn't matter. It doesn't handle malformed input well, but neither did the old version. That's for another exercise, maybe. -Adam