Forum: Ruby Help with "rubyizing" script

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sebastian Steinlechner (Guest)
on 2006-02-05 17:47
(Received via mailing list)
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
Matthew M. (Guest)
on 2006-02-06 17:23
(Received via mailing list)
> 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?

(0..x) forms a range object which can be rather useful.  If you don't
like that, you can try 0.upto(x), which is less compact but maybe will
look better for you? I don't know anything offhand for a cartiesian
product, but it wouldn't be that difficult to write such a method that
yielded to a block.


> 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?

Ruby makes extending classes easy...


class Numeric
  def sqrt
    Math.sqrt(self)
  end
end

3.sqrt
=> 1.73205080756888
This topic is locked and can not be replied to.