class SpecialRatio::Ratio

Constants

VALID_KEYS

Public Class Methods

new(params) click to toggle source
# File lib/special_ratio/ratio.rb, line 5
def initialize(params)
  params = params.select { |k, v| VALID_KEYS.include?(k) }

  unless params.size == 1
    raise ArgumentError, ':long or :short or :sum is required'
  end

  @key, @value = params.map { |k, v| [k, v] } .flatten
end

Public Instance Methods

long() click to toggle source
# File lib/special_ratio/ratio.rb, line 15
def long
  case @key
  when :long
    @value
  when :short
    @value * @ratio
  when :sum
    @value * (@ratio / (@ratio + 1))
  end
end
short() click to toggle source
# File lib/special_ratio/ratio.rb, line 26
def short
  case @key
  when :long
    @value / @ratio
  when :short
    @value
  when :sum
    @value / (@ratio + 1)
  end
end
sum() click to toggle source
# File lib/special_ratio/ratio.rb, line 37
def sum
  case @key
  when :long
    @value * ((@ratio + 1) / @ratio)
  when :short
    @value * (@ratio + 1)
  when :sum
    @value
  end
end