How do I get the original numbers? For example when I type:
r = Rational(2, 10)
# (1/5)
2 and 10 will be changed to 1 and 5:
r.numerator # 1
r.denominator # 5
How do I get 2 & 10 from instance of Rational class(r
)?
I monkey-patched Rational class and created new method(Rational_o
):
def Rational_o *args
x, y = args
r = Rational *args
r.x = x
r.y = y
r
end
class Rational
attr_accessor :x, :y
end
It works, but is there build-in method or variable(s) where original x & y are stored?
No, there isn't. Reduction is a basic and common way to normalize rational numbers. Why would a rational number keep the original numerator and denominator? It does not make sense.
Your question is like asking "Does a string created by
"foo" + "bar"
(which becomes"foobar"
) keep the original substrings"foo"
and"bar"
? Where are they stored?"If you really want to keep the original numbers, then a rational number is not what you want, and subclassing
Rational
is not the right way to go. You should use an array holding a pair of numbers.