I was a bit surprised when I ran the following

irb(main):001:0> include Math
=> Object
irb(main):002:0> sqrt(Rational(16,9))
=> 1.3333333333333333

I was able to correct this as follows:

include Math

class Integer
  def square?
    sqrt(self).round ** 2 == self
  end
end

module Math
  alias _sqrt_ sqrt
  def sqrt(x)
    if x.instance_of?(Rational)
       n = x.numerator
       d = x.denominator
      return Rational(sqrt(n), sqrt(d)) if n.square? && d.square?
    end
    _sqrt_ x
  end
end

Now it works:

irb(main):032:0> sqrt(Rational(16,9))
=> (4/3)
irb(main):033:0> sqrt(Rational(16,8))
=> 1.4142135623730951

-- 
Posted via http://www.ruby-forum.com/.