Stefan Rusterholz wrote:
> Whoops, that was the old code snippet...
>
> def Rational(a, b = 1)
> 	Rational.reduce(*Rational.normalize(a,b))
> end
>
> class Rational < Numeric
> 	# normalizes numerator/denominator to non-rationals
> 	def Rational.normalize(a,b)
> 		if a.kind_of?(Rational) then
> 			if b == 1 then
> 				return [a.nominator, a.denominator]
> 			elsif b.kind_of?(Rational) then
> 				return [a.numerator*b.denominator, a.denominator*b.numerator]
> 			else
> 				return [a.numerator, a.denominator*b]
> 			end
> 		elsif b.kind_of?(Rational) then
> 			if a == 1 then
> 				return [b.denominator, b.nominator]
> 			else
> 				return [a*b.denominator, b.numerator]
> 			end
> 		else
> 			return [a,b]
> 		end
> 	end
>
>   def Rational.new!(num, den = 1)
>     new(*Rational.normalize(num,den))
>   end
> end
>
> Should work a bit better ;-)
>
> --
> Posted via http://www.ruby-forum.com/.

Tested the code and it appears to do it correctly

add /substact rationals
raise rational to rational
add/substract rational and integer
add/substract  rational and float


About the only thing I tried but could not do would be rational to mix
number

Rational(11,7).to_mix  => "11 4/7"