class EtsiiGem::Fraccion

Attributes

denom[RW]
num[RW]

Public Class Methods

new(a, b) click to toggle source
# File lib/ETSII_GEM.rb, line 96
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

Public Instance Methods

*(other) click to toggle source
# File lib/ETSII_GEM.rb, line 146
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) click to toggle source
# File lib/ETSII_GEM.rb, line 128
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) click to toggle source
# File lib/ETSII_GEM.rb, line 137
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) click to toggle source
# File lib/ETSII_GEM.rb, line 155
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
coerce(other) click to toggle source
# File lib/ETSII_GEM.rb, line 165
def coerce(other)
    [self,other]
end
mcd(u, v) click to toggle source
# File lib/ETSII_GEM.rb, line 112
def mcd(u, v)
   u, v = u.abs, v.abs
   while v != 0
      u, v = v, u % v
   end
   u
end
to_f() click to toggle source
# File lib/ETSII_GEM.rb, line 124
def to_f
    @num.to_f/@denom.to_f
end
to_s() click to toggle source
# File lib/ETSII_GEM.rb, line 120
def to_s
    "#{@num}/#{@denom}"
end