class Percentage

Attributes

value[R]

Public Class Methods

change(a, b) click to toggle source
# File lib/percentage.rb, line 117
def Percentage.change(a, b)
  Percentage.new((b - a).to_r / a)
end
new(value) click to toggle source
# File lib/percentage.rb, line 8
def initialize(value)
  @value = value
end

Public Instance Methods

*(object) click to toggle source
# File lib/percentage.rb, line 60
def *(object)
  case object
  when self.class
    self.class.new(fractional_value.to_r * object.fractional_value)
  else
    fractional_value.coerce(object).inject(&:*)
  end
end
+(object) click to toggle source
# File lib/percentage.rb, line 48
def +(object)
  if self.class === object
    if @value.integer? ^ object.value.integer?
      self.class.new(fractional_value + object.fractional_value)
    else
      self.class.new(@value + object.value)
    end
  else
    raise TypeError, "cannot add #{object.class} to #{self.class}"
  end
end
<=>(object) click to toggle source
# File lib/percentage.rb, line 32
def <=>(object)
  if self.class === object
    fractional_value <=> object.fractional_value
  elsif Numeric === object
    fractional_value <=> object
  end
end
coerce(other) click to toggle source
# File lib/percentage.rb, line 69
def coerce(other)
  case other
  when Numeric
    return fractional_value, other
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end
encode_with(coder) click to toggle source
# File lib/percentage/yaml.rb, line 4
def encode_with(coder)
  coder.represent_scalar(nil, to_s)
end
eql?(object) click to toggle source
# File lib/percentage.rb, line 40
def eql?(object)
  object.instance_of?(self.class) && @value.eql?(object.value)
end
hash() click to toggle source
# File lib/percentage.rb, line 44
def hash
  @value.hash
end
scale(n) click to toggle source
# File lib/percentage.rb, line 90
def scale(n)
  self.class.new(@value * n)
end
to_f() click to toggle source
# File lib/percentage.rb, line 16
def to_f
  (fractional_value * 100).to_f
end
to_i() click to toggle source
# File lib/percentage.rb, line 12
def to_i
  (fractional_value * 100).to_i
end
to_r() click to toggle source
# File lib/percentage.rb, line 24
def to_r
  fractional_value.to_r
end
to_s() click to toggle source
# File lib/percentage.rb, line 20
def to_s
  "#{string_value}%"
end
truncate(ndigits = nil) click to toggle source
# File lib/percentage.rb, line 78
def truncate(ndigits = nil)
  return self if @value.integer?

  value = @value * 100

  if ndigits.nil?
    self.class.new(value.truncate)
  else
    self.class.new(value.truncate(ndigits) / 100)
  end
end
zero?() click to toggle source
# File lib/percentage.rb, line 28
def zero?
  @value.zero?
end

Protected Instance Methods

fractional_value() click to toggle source
# File lib/percentage.rb, line 96
def fractional_value
  @value.integer? ? Rational(@value, 100) : @value
end

Private Instance Methods

string_value() click to toggle source
# File lib/percentage.rb, line 102
def string_value
  if @value.integer?
    @value.to_s
  elsif BigDecimal === @value
    (@value * 100).to_s('F')
  else
    (@value * 100).to_f.to_s
  end
end