From: James Edward Gray II Date: 2006-06-16T06:05:58+09:00 Subject: Re: Newbie program On Jun 15, 2006, at 1:34 PM, Logesh Pillay wrote: > I'd appreciate suggestions how to improve it. Well, I've tried to Rubify the code a bit. I did these things; * Stopped defining methods in methods. * Used a class. * Switched the global variables to instance variables. * Used upto() iterators. * Got rid of print(). * Made stillgood() method name more Rubish. * Added the application code test at the end. The thing I think you should still do: * Pick more meaningful variable names than @n and k. Here's the code. class NQueens def initialize @n = 8 @board = [nil] * (@n + 1) end def choose(k) if k > @n puts @board.join else 1.upto(@n) do |i| @board[k] = i choose(k + 1) if k == 1 or still_good?(k) end end end def still_good?(k) 1.upto(k - 1) do |i| return false if (@board[k] == @board[i]) or (k-i == (@board[k] - @board[i]).abs) end true end end if __FILE__ == $PROGRAM_NAME NQueens.new.choose(1) end Hope that helps. James Edward Gray II