Class: Fraccion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/matrix_expansion/fraccion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Fraccion

Returns a new instance of Fraccion.



6
7
8
9
10
11
12
13
14
# File 'lib/matrix_expansion/fraccion.rb', line 6

def initialize(a,b)
        #comprobamos que los datos de entradas sean numericos
        raise unless a.is_a?(Numeric)
        raise unless b.is_a?(Numeric)
        #comprobamos que el dominador no sea cero 
        raise unless (b != 0)
        #inicializamos las variables
         @a, @b = a, b
end

Instance Attribute Details

#aObject (readonly)

a: numerador, b: denominador



5
6
7
# File 'lib/matrix_expansion/fraccion.rb', line 5

def a
  @a
end

#bObject (readonly)

a: numerador, b: denominador



5
6
7
# File 'lib/matrix_expansion/fraccion.rb', line 5

def b
  @b
end

Instance Method Details

#%(other) ⇒ Object

Resto de fracciones



100
101
102
103
104
105
106
107
# File 'lib/matrix_expansion/fraccion.rb', line 100

def % (other)
        r = Fraccion.new(0,1) #Inicializa objeto
        r = self
        while (r.to_f >= other.to_f) do   #Hasta que no se pueda restar más. Queda en r el resto.
                r = r - other
        end
        return r        
end

#*(other) ⇒ Object

Producto de fracciones



80
81
82
83
84
85
86
87
# File 'lib/matrix_expansion/fraccion.rb', line 80

def * (other)
        a = @a * other.num
        b = @b * other.denom
        k = gcd(a,b)
        a /= k
        b /= k
        Fraccion.new(a,b)
end

#+(other) ⇒ Object

Suma de fracciones



44
45
46
47
48
49
50
51
52
53
# File 'lib/matrix_expansion/fraccion.rb', line 44

def + (other)
        comun = @b * other.denom
        a1 = @a * other.denom
        a2 = other.num * @b
        a3 = a1 + a2
        k = gcd(a3,comun)
        a3 /= k
        comun /= k
        Fraccion.new(a3,comun)
end

#-(other) ⇒ Object

Resta de fracciones



56
57
58
59
60
61
62
63
64
65
# File 'lib/matrix_expansion/fraccion.rb', line 56

def - (other)
        comun = @b * other.denom
        a1 = @a * other.denom
        a2 = other.num * @b
        a3 = a1 - a2
        k = gcd(a3,comun)
        a3 /= k
        comun /= k
        Fraccion.new(a3,comun)
end

#-@Object

Opuesto de fracción. La suma de una fracción y su opuesto debe ser 0.



68
69
70
71
72
73
74
75
76
77
# File 'lib/matrix_expansion/fraccion.rb', line 68

def -@
        k = gcd(@a, @b)
a = @a / k
b = @b / k
if (b < 0)  #Si el denominador es negativo
                return Fraccion.new(a,-b)
        else 
                return Fraccion.new(-a,b)
        end
end

#/(other) ⇒ Object

División de fracciones



90
91
92
93
94
95
96
97
# File 'lib/matrix_expansion/fraccion.rb', line 90

def / (other)
        a = @a * other.denom
        b = @b * other.num
        k = gcd(a,b)
        a /= k
        b /= k
        Fraccion.new(a,b)
end

#<=>(other) ⇒ Object



110
111
112
# File 'lib/matrix_expansion/fraccion.rb', line 110

def <=> (other)
        self.to_f <=> other.to_f
end

#absObject

Fracción pasada a float. (ej: 1.25)



34
35
36
# File 'lib/matrix_expansion/fraccion.rb', line 34

def abs
         f= Fraccion.new(@a.abs, @b.abs)
end

#coerce(other) ⇒ Object



114
115
116
# File 'lib/matrix_expansion/fraccion.rb', line 114

def coerce (other)
    [Fraccion.new(other,1),self]
end

#denomObject

metedo que retorna el denominador



25
26
27
# File 'lib/matrix_expansion/fraccion.rb', line 25

def denom
        return @b
end

#numObject

metodo que retorna el numerador



21
22
23
# File 'lib/matrix_expansion/fraccion.rb', line 21

def num
        return @a
end

#reciprocalObject

Fracción reciproca o inversa (a/b -> b/a)



39
40
41
# File 'lib/matrix_expansion/fraccion.rb', line 39

def reciprocal
        f= Fraccion.new(@b, @a)
end

#to_fObject

Fraccion en modo flotante



29
30
31
# File 'lib/matrix_expansion/fraccion.rb', line 29

def to_f
        @a.to_f / @b.to_f #conversion de tipo a flotante mediante to_f
end

#to_sObject

formateo de la salida (a/b)



16
17
18
# File 'lib/matrix_expansion/fraccion.rb', line 16

def to_s
        "#{@a}/#{@b}"
end