class Fraccion

Attributes

d[R]
n[R]

Public Class Methods

new(n, d) click to toggle source

Constructor de la clase Fraccion

# File lib/Fraccion.rb, line 17
def initialize(n, d)
   @n = n / mcd(n,d)
   @d = d / mcd(n,d)
end

Public Instance Methods

%(other) click to toggle source

Sobrecarga del operador % para operar con fracciones

# File lib/Fraccion.rb, line 56
def % (other)
     Fraccion.new((imprimirFlotante % other.imprimirFlotante*1000).to_i, 1000)
end
*(other) click to toggle source

Sobrecarga del operador * para operar con fracciones

# File lib/Fraccion.rb, line 48
def * (other)
     Fraccion.new(@n* other.n, @d * other.d)
end
+(other) click to toggle source

Sobrecarga del operador + para operar con fracciones

# File lib/Fraccion.rb, line 40
def + (other)
     Fraccion.new(@n* other.d + other.n*@d, @d * other.d)
end
-(other) click to toggle source

Sobrecarga del operador - para operar con fracciones

# File lib/Fraccion.rb, line 44
def - (other)
     Fraccion.new(@n* other.d - other.n*@d, @d * other.d)
end
-@() click to toggle source

Calcula el opuesto de una fraccion

# File lib/Fraccion.rb, line 89
def -@
     Fraccion.new(@n*-1, @d)
end
/(other) click to toggle source

Sobrecarga del operador / para operar con fracciones

# File lib/Fraccion.rb, line 52
def / (other)
     Fraccion.new(@n* other.d, @d * other.n)
end
<=>(other) click to toggle source

Sobrecarga del <=> para poder comparar fracciones (==, <=, >=, …)

# File lib/Fraccion.rb, line 60
def <=> (other)
     imprimirFlotante <=> other.imprimirFlotante
end
abs() click to toggle source

Calcula el valor absoluto

# File lib/Fraccion.rb, line 65
def abs
     if (@n < 0) ^ (@d < 0)
        if @n < 0
             Fraccion.new(@n*-1, @d)
        else
             Fraccion.new(@n, @d*-1)
        end

             elsif (@n < 0) && (@d < 0)
                     Fraccion.new(@n*-1, @d*-1)

                   else
                      Fraccion.new(@n, @d)
             end
  end
coerce(other) click to toggle source
# File lib/Fraccion.rb, line 32
def coerce(other)
  if (other.is_a?(Numeric))
    [Fraccion.new(other.to_i,1),self]
  end
end
imprimirFlotante() click to toggle source

Pasa a flotante el objeto Fraccion

# File lib/Fraccion.rb, line 28
def imprimirFlotante
   @n.to_f/@d.to_f
end
imprimirFraccion() click to toggle source

Pasa a string el objeto Fraccion

# File lib/Fraccion.rb, line 23
def imprimirFraccion
   "#{@n}/#{@d}" 
end
reciprocal() click to toggle source

Calcula el inverso de una fraccion

# File lib/Fraccion.rb, line 83
def reciprocal
     Fraccion.new(@d, @n)
end