Class: Kwyjibo::Fraccion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b) ⇒ Fraccion



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/kwyjibo.rb', line 196

def initialize(a, b)
    x = mcd(a,b)
    @num = a/x
    @denom = b/x

    if (@num < 0 && @denom < 0)
        @num = @num * -1
        @denom = @denom * -1
    end

    if (@denom < 0)
        @denom = @denom * -1
        @num = @num * -1
    end
end

Instance Attribute Details

#denomObject

Returns the value of attribute denom.



194
195
196
# File 'lib/kwyjibo.rb', line 194

def denom
  @denom
end

#numObject

Returns the value of attribute num.



194
195
196
# File 'lib/kwyjibo.rb', line 194

def num
  @num
end

Instance Method Details

#%(other) ⇒ Object



264
265
266
267
268
269
270
271
272
# File 'lib/kwyjibo.rb', line 264

def %(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        division = Fraccion.new(@num * c.denom, @denom * c.num)
    else
        division = Fraccion.new(@num * other.denom, @denom * other.num)
    end  
    division.num % division.denom
end

#*(other) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/kwyjibo.rb', line 246

def *(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.num, @denom * other.denom)
    end
end

#+(other) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/kwyjibo.rb', line 228

def +(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom + @denom * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.denom + @denom * other.num, @denom * other.denom)
    end
end

#-(other) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/kwyjibo.rb', line 237

def -(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom - @denom * c.num, @denom * c.denom)
    else
        Fraccion.new(@num * other.denom - @denom * other.num, @denom * other.denom)
    end
end

#-@Object



285
286
287
288
289
# File 'lib/kwyjibo.rb', line 285

def -@
    if (@num > 0)
        @num = @num * -1
    end
end

#/(other) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/kwyjibo.rb', line 255

def /(other)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        Fraccion.new(@num * c.denom, @denom * c.num)
    else
        Fraccion.new(@num * other.denom, @denom * other.num)
    end        
end

#<=>(other) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/kwyjibo.rb', line 291

def <=>(other)
    return nil unless (other.instance_of? Fraccion) || (other.instance_of? Fixnum)
    if other.instance_of? Fixnum
        c = Fraccion.new(other,1)
        (c.num.to_f/c.denom.to_f) <=> (self.num.to_f/self.denom.to_f)
    else
        (self.num.to_f/self.denom.to_f) <=> (other.num.to_f/other.denom.to_f)
    end
end

#absObject



274
275
276
277
# File 'lib/kwyjibo.rb', line 274

def abs
    @num = @num.abs
    @denom = @denom.abs
end

#coerce(other) ⇒ Object



301
302
303
# File 'lib/kwyjibo.rb', line 301

def coerce(other)
    [self,other]
end

#mcd(u, v) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/kwyjibo.rb', line 212

def mcd(u, v)
   u, v = u.abs, v.abs
   while v != 0
      u, v = v, u % v
   end
   u
end

#reciprocalObject



279
280
281
282
283
# File 'lib/kwyjibo.rb', line 279

def reciprocal
    x = @num
    @num = @denom
    @denom = x
end

#to_fObject



224
225
226
# File 'lib/kwyjibo.rb', line 224

def to_f
    @num.to_f/@denom.to_f
end

#to_sObject



220
221
222
# File 'lib/kwyjibo.rb', line 220

def to_s
    "#{@num}/#{@denom}"
end