Class: Kwyjibo::Fraccion
Instance Attribute Summary collapse
-
#denom ⇒ Object
Returns the value of attribute denom.
-
#num ⇒ Object
Returns the value of attribute num.
Instance Method Summary collapse
- #%(other) ⇒ Object
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #abs ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(a, b) ⇒ Fraccion
constructor
A new instance of Fraccion.
- #mcd(u, v) ⇒ Object
- #reciprocal ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
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
#denom ⇒ Object
Returns the value of attribute denom.
194 195 196 |
# File 'lib/kwyjibo.rb', line 194 def denom @denom end |
#num ⇒ Object
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 |
#abs ⇒ Object
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 |
#reciprocal ⇒ Object
279 280 281 282 283 |
# File 'lib/kwyjibo.rb', line 279 def reciprocal x = @num @num = @denom @denom = x end |
#to_f ⇒ Object
224 225 226 |
# File 'lib/kwyjibo.rb', line 224 def to_f @num.to_f/@denom.to_f end |
#to_s ⇒ Object
220 221 222 |
# File 'lib/kwyjibo.rb', line 220 def to_s "#{@num}/#{@denom}" end |