# Help with "rubyizing" script

**URL:** <https://www.ruby-forum.com/t/help-with-rubyizing-script/53438>\
**Category:** Ruby\
**Created:** [February 5, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/help-with-rubyizing-script/53438 "2006-02-05T16:47:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Steinlech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Sebastian\_Steinlech](https://www.ruby-forum.com/u/Sebastian_Steinlech)\
**Post date:** [February 5, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/help-with-rubyizing-script/53438/1 "2006-02-05T16:47:25Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Steinlech](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Sebastian\_Steinlech](https://www.ruby-forum.com/u/Sebastian_Steinlech)\
**Post date:** [February 6, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/help-with-rubyizing-script/53438/2 "2006-02-06T16:23:58Z")

</div>

> 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!:  
> [module Math - RDoc Documentation](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
