From: Sebastian Steinlechner Date: 2006-02-06T00:44:19+09:00 Subject: Help with "rubyizing" script Hi list, I haven't gotten around to do some serious programming with Ruby, but today I decided to hack up a quick script to help me with calculating Miller's indices (if you don't know what that is, doesn't matter...). Unfortunately, it didn't come out quite as nicely as I'd thought/hoped. Well, here it is: -- snip -- a = gets.to_i w = Math.sqrt(a).to_i (0..w).each do |l| (0..l).each do |k| (0..k).each do |h| print a, ': (', h,k,l,")\n" if (h*h + k*k + l*l) == a end end end -- snap -- First of all, the to_i stuff at the beginning is somewhat ugly. It probably works without, but I first used w.times, which needs a Fixnum... then again, a needs to be Fixnum for the if statement to work. Then I don't really like the (0..x) stuff. As I said, I tried times, but that only counts up to w-1. Hm. And (w+1).times is even uglier. Anything better than that? There's probably a better way get a list of all members of [0..w] x [0..w] x [0..w] ("x" being the cartesian product), from which one could then select() the ones that fit. Any ideas on that? Then I don't like the print statement. I thought print "#{a}: (#{h,k,l})" might work, but that gives me an error (unterminated string). Why's that? And print "#{a}: (#{h}#{k}#{l})" is again quite clumsy, I'd say. Oh, and a last thing: I'd rather write a.sqrt and was disappointed this doesn't work. However, ruby-doc _does_ mention Math#sqrt, and even Math#sqrt!: http://ruby-doc.org/core/classes/Math.html#M002069 It took me a while to find out those are from Complex. Would it be possible to state this more clearly somewhere? Better yet, make a.sqrt work? Thanks in advance, Sebastian