class Ratio

The ratio between two numbers (eg: 2:1, 3:4)

Can be compared, added, “percent”ed, “to_f”ed, and displayed.

Attributes

first[RW]
last[RW]

Public Class Methods

[](*args) click to toggle source
# File lib/epitools/ratio.rb, line 16
def self.[](*args)
  new(*args)
end
new(first, last=1) click to toggle source

`first` is the top part of the ratio, `last` is the bottom (eg: `first/last`)

# File lib/epitools/ratio.rb, line 23
def initialize(first, last=1)
  @first = first
  @last = last
end

Public Instance Methods

+(other) click to toggle source

Adds together the tops and bottoms of the ratios.

Example: For the ratios `a/c` and `b/d`, returns `a+b/c+d`

# File lib/epitools/ratio.rb, line 67
def +(other)
  Ratio.new( first+other.first, last+other.last)
end
<=>(other) click to toggle source
# File lib/epitools/ratio.rb, line 10
def <=>(other)
  to_f <=> other.to_f
end
inspect() click to toggle source

“#<Ratio: 1/2>”

# File lib/epitools/ratio.rb, line 58
def inspect
  "#<Ratio: #{to_s}>"
end
percent() click to toggle source

Returns a string representing the number in percent

# File lib/epitools/ratio.rb, line 50
def percent
  "%0.1f%%" % (to_f * 100)
end
Also aliased as: to_percent
ratio()
Alias for: to_s
to_f() click to toggle source

Returns the ratio as a float. (eg: Ratio.to_f == 0.5)

# File lib/epitools/ratio.rb, line 39
def to_f
  if @last == 0
    0.0
  else
    @first.to_f / @last
  end
end
to_percent()
Alias for: percent
to_s() click to toggle source

Returns a string representation: “a/b”

# File lib/epitools/ratio.rb, line 31
def to_s
  "#{@first}/#{@last}"
end
Also aliased as: ratio