Class: Fraccion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, d) ⇒ Fraccion

Returns a new instance of Fraccion.



9
10
11
12
# File 'lib/Fraccion.rb', line 9

def initialize (n,d)
        @n=n
        @d=d
end

Instance Attribute Details

#dObject (readonly)

Returns the value of attribute d.



8
9
10
# File 'lib/Fraccion.rb', line 8

def d
  @d
end

#nObject (readonly)

Returns the value of attribute n.



8
9
10
# File 'lib/Fraccion.rb', line 8

def n
  @n
end

Instance Method Details

#*(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/Fraccion.rb', line 96

def * (other)
        if other.instance_of?Fixnum
                other = Fraccion.new(other,1)
                n=(@n *(other.n))
                d=(@d*1)
                
        else
                n=(@n*other.n)
                d=(@d*other.d)

        end
        f4=Fraccion.new(n,d)
        f4.min
        #"#{f4.n/f4.d}"
        return f4

end

#+(other) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/Fraccion.rb', line 66

def + (other)
        if other.instance_of?Fixnum
                n=(@n*1+(@d*other))
                d=(@d*1)
                
        else
                n=(@n*other.d)+(@d*other.n)
                d=(@d*other.d)
        end
        #"#{f4.n/f4.d}"
        f4=Fraccion.new(n,d)
        f4.min
        return f4
end

#-(other) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/Fraccion.rb', line 81

def - (other)
       if other.instance_of?Fixnum
                n=(@n*1-(@d*other))
                d=(@d*1)
                
        else
                n=(@n*other.d)-(@d*other.n)
                d=(@d*other.d)
        end
        #"#{f4.n/f4.d}"
        f4=Fraccion.new(n,d)
        f4.min
        return f4
end

#/(other) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/Fraccion.rb', line 113

def / (other)
        n=@n*other.d
        d=@d*other.n
        f4=Fraccion.new(n,d)
        f4.min
        "#{f4.n/f4.d}"
end

#<=>(other) ⇒ Object



120
121
122
123
# File 'lib/Fraccion.rb', line 120

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

#absObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/Fraccion.rb', line 45

def abs
        n = @n
        d = @d

        if @n < 0
                n = -@n
        end

        if @d < 0
                d = -@d
        end
        Fraccion.new(n, d)
end

#coerce(other) ⇒ Object



125
126
127
# File 'lib/Fraccion.rb', line 125

def coerce(other)
        return [self,other]
end

#getdObject



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

def getd
        return @d
end

#getnObject



30
31
32
# File 'lib/Fraccion.rb', line 30

def getn
        return @n
end

#minObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/Fraccion.rb', line 19

def min 
        a=gcd(@n,@d)

        @n = @n/a
        @d = @d/a

        if gcd(@n, @d) > 1
                self.min
        end
end

#opuestoObject



63
64
65
# File 'lib/Fraccion.rb', line 63

def opuesto
        Fraccion.new(-@n,@d)
end

#reciprocalObject



59
60
61
# File 'lib/Fraccion.rb', line 59

def reciprocal
        Fraccion.new(@d,@n)
end

#to_fObject



38
39
40
41
42
43
# File 'lib/Fraccion.rb', line 38

def to_f
        #puts "Flotante: #{@n.to_f/@d.to_f}"
        "#{@n.to_f/@d.to_f}"
        #f4 = Fraccion.new(@n.to_f, @d.to_f)
        #return f4
end

#to_sObject



14
15
16
17
# File 'lib/Fraccion.rb', line 14

def to_s
        
        "#{@n}/#{@d}"                
end