class Fraccion
Attributes
d[RW]
n[RW]
Public Class Methods
new(n,d)
click to toggle source
# File lib/matrixfamily/fraccion.rb, line 7 def initialize(n,d) @n, @d = n, d end
Public Instance Methods
*(other)
click to toggle source
metodo multiplicacion de dos fracciones
# File lib/matrixfamily/fraccion.rb, line 74 def *(other) @fraccion = Fraccion.new((@n*other.n),@d*other.d) reducir(@fraccion) end
+(other)
click to toggle source
metodo de suma de dos fracciones
# File lib/matrixfamily/fraccion.rb, line 86 def +(other) mcm = (@d * other.d)/gcd(@d, other.d) @fraccion = Fraccion.new(((mcm/@d*@n) + (mcm/other.d*other.n)),mcm) reducir(@fraccion) end
-(other)
click to toggle source
metodo de resta de dos fracciones
# File lib/matrixfamily/fraccion.rb, line 93 def -(other) mcm = (@d * other.d)/gcd(@d, other.d) @fraccion = Fraccion.new(((mcm/@d*@n) - (mcm/other.d*other.n)),mcm) reducir(@fraccion) end
/(other)
click to toggle source
metodo de division de dos fracciones
# File lib/matrixfamily/fraccion.rb, line 80 def /(other) @fraccion = Fraccion.new((@n*other.d),@d*other.n) reducir(@fraccion) end
<=>(other)
click to toggle source
método necesario con la librería Comparable
# File lib/matrixfamily/fraccion.rb, line 100 def <=>(other) mcm = (@d * other.d)/gcd(@d, other.d) a = (mcm/@d*@n) b = (mcm/other.d*other.n) if a < b -1 elsif a > b 1 else 0 end end
==(other)
click to toggle source
metodo == –> para comparar 2 fracciones
# File lib/matrixfamily/fraccion.rb, line 46 def ==(other) if @n==other.n && @d==other.d then true else false end end
abs()
click to toggle source
metodo abs –> Valor absoluto
# File lib/matrixfamily/fraccion.rb, line 55 def abs #Si el numerador es menor que 0, le quitamos lo negativo multiplicando por -1 if @n < 0 then @n = @n * -1 end #Si el denominador es menor que 0, le quitamos lo negativo multiplicando por -1 if @d < 0 then @d = @d * -1 end end
coerce(other)
click to toggle source
definicion del metodo coerce para racional
# File lib/matrixfamily/fraccion.rb, line 115 def coerce(other) if Integer === other [Fraccion.new(other,1), self] end end
denom()
click to toggle source
metodo denom
# File lib/matrixfamily/fraccion.rb, line 23 def denom @d end
flotante()
click to toggle source
metodo flotante
# File lib/matrixfamily/fraccion.rb, line 41 def flotante @n.to_f/@d.to_f #conversion de tipo a flotante mediante to_f end
gcd(u, v)
click to toggle source
# File lib/matrixfamily/fraccion.rb, line 32 def gcd(u, v) u, v = u.abs, v.abs while v != 0 u, v = v, u % v end u end
num()
click to toggle source
metodo num
# File lib/matrixfamily/fraccion.rb, line 18 def num @n end
reciprocal()
click to toggle source
metodo reciprocal –> invertir numerador por denominador y viceversa
# File lib/matrixfamily/fraccion.rb, line 67 def reciprocal x = @n @n = @d @d = x end
reducir(frac)
click to toggle source
metodo reducir
# File lib/matrixfamily/fraccion.rb, line 12 def reducir(frac) mcd = gcd(frac.n,frac.d) fraccion = Fraccion.new((frac.n/mcd),frac.d/mcd) end
to_s()
click to toggle source
metodo string
# File lib/matrixfamily/fraccion.rb, line 28 def to_s "#{@n}/#{@d}" end