From: Dan Connelly Date: 2012-06-15T00:37:36+09:00 Subject: Re: sqrt of rational numbers Looking at mathn.rb was instructive. 1. I forgot module_function -- this is needed when modifying Math functions to have "include Math" work correctly. 2. It uses "kind_of?" instead of "instance_of?". This allows compatibility with sub-classes or Rational. I suppose "x.responds_to?(:numerator) && x.responds_to?(:denominator)" would also have worked, but I'm not sure if I'm a huge fan of "duck typing", since it isn't obvious that every class which defines these functions is using them in a compatible fashion. "kind_of?" is safer. 3. mathn solves the square root of integer problem ( sqrt(16) = 4, not sqrt(16) = 4.0), then redefines division of integers to produce a Rational (if not an exact integer), then simply uses square root on the numerator and denominator of the Rational. So it's more comprehensive. So the answer is: if you want these things to be well handled then use "mathn", albeit at the expense of speed. -- Posted via http://www.ruby-forum.com/.