lib/lite/ruby/numeric.rb in lite-ruby-1.3.3 vs lib/lite/ruby/numeric.rb in lite-ruby-2.0.0

- old
+ new

@@ -1,244 +1,242 @@ # frozen_string_literal: true -if Lite::Ruby.configuration.monkey_patches.include?('numeric') - class Numeric +class Numeric - def add(num) - self + num - end + def add(num) + self + num + end - def at_least(lower) - self >= lower ? self : lower - end + def at_least(lower) + self >= lower ? self : lower + end - def at_most(upper) - self <= upper ? self : upper - end + def at_most(upper) + self <= upper ? self : upper + end - def close?(number, epsilon = 0.01) - return self == number if epsilon.zero? + def close?(number, epsilon = 0.01) + return self == number if epsilon.zero? - a = to_f - b = number.to_f + a = to_f + b = number.to_f - if a.zero? || b.zero? - (a - b).abs < epsilon - else - (a / b - 1).abs < epsilon - end + if a.zero? || b.zero? + (a - b).abs < epsilon + else + (a / b - 1).abs < epsilon end + end - def decrement(amount = 1.0) - self - amount - end + def decrement(amount = 1.0) + self - amount + end - def delimit(options = {}) - delimiter = options[:delimiter] || ',' - separator = options[:separator] || '.' + def delimit(options = {}) + delimiter = options[:delimiter] || ',' + separator = options[:separator] || '.' - digits, decimals = to_s.split('.') - digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse + digits, decimals = to_s.split('.') + digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse - return digits unless decimals + return digits unless decimals - [digits, decimals].join(separator) - end + [digits, decimals].join(separator) + end - def delta(num) - (self - num).abs - end + def delta(num) + (self - num).abs + end - def distance(num) - self - num - end + def distance(num) + self - num + end - def divide(num) - return num if num.zero? + def divide(num) + return num if num.zero? - self / num - end + self / num + end - def equal_to?(num) - self == num - end + def equal_to?(num) + self == num + end - def fraction - (self - truncate).abs - end + def fraction + (self - truncate).abs + end - def fraction? - fraction.to_d != 0.0.to_d - end + def fraction? + fraction.to_d != 0.0.to_d + end - def greater_than?(num) - num < self - end + def greater_than?(num) + num < self + end - def greater_than_or_equal_to?(num) - num <= self - end + def greater_than_or_equal_to?(num) + num <= self + end - def increment(amount = 1.0) - self + amount - end + def increment(amount = 1.0) + self + amount + end - def inside?(start, finish) - (start < self) && (finish > self) - end + def inside?(start, finish) + (start < self) && (finish > self) + end - def less_than?(num) - num > self - end + def less_than?(num) + num > self + end - def less_than_or_equal_to?(num) - num >= self - end + def less_than_or_equal_to?(num) + num >= self + end - def markdown_percentage(percent) - to_f * ((100.0 - percent.to_f) / 100.0) - end + def markdown_percentage(percent) + to_f * ((100.0 - percent.to_f) / 100.0) + end - def markup_percentage(percent) - to_f + (to_f * (percent.to_f / 100.0)) - end + def markup_percentage(percent) + to_f + (to_f * (percent.to_f / 100.0)) + end - def multiply(num) - self * num - end + def multiply(num) + self * num + end - def multiple_of?(number) - return zero? if number.zero? + def multiple_of?(number) + return zero? if number.zero? - modulo(number).zero? - end + modulo(number).zero? + end - def not_equal_to?(num) - self != num - end + def not_equal_to?(num) + self != num + end - def ordinal - return 'th' if (11..13).cover?(abs % 100) + def ordinal + return 'th' if (11..13).cover?(abs % 100) - case abs % 10 - when 1 then 'st' - when 2 then 'nd' - when 3 then 'rd' - else 'th' - end + case abs % 10 + when 1 then 'st' + when 2 then 'nd' + when 3 then 'rd' + else 'th' end + end - def ordinalize - "#{self}#{ordinal}" - end + def ordinalize + "#{self}#{ordinal}" + end - def outside?(start, finish) - (start > self) || (finish < self) - end + def outside?(start, finish) + (start > self) || (finish < self) + end - def pad(options = {}) - pad_number = options[:pad_number] || 0 - precision = options[:precision] || 3 + def pad(options = {}) + pad_number = options[:pad_number] || 0 + precision = options[:precision] || 3 - to_s.rjust(precision, pad_number.to_s) - end + to_s.rjust(precision, pad_number.to_s) + end - # rubocop:disable Metrics/AbcSize, Metrics/MethodLength - def pad_precision(options = {}) - pad_number = options[:pad_number] || 0 - precision = options[:precision] || 2 - separator = options[:separator] || '.' - string = to_s + # rubocop:disable Metrics/AbcSize, Metrics/MethodLength + def pad_precision(options = {}) + pad_number = options[:pad_number] || 0 + precision = options[:precision] || 2 + separator = options[:separator] || '.' + string = to_s - string << separator unless string.include?(separator) - ljust_count = string.split(separator).first.length - ljust_count += (string.count(separator) + precision) if precision.positive? + string << separator unless string.include?(separator) + ljust_count = string.split(separator).first.length + ljust_count += (string.count(separator) + precision) if precision.positive? - if ljust_count >= string.length - string.ljust(ljust_count, pad_number.to_s) - else - string[0..(ljust_count - 1)] - end + if ljust_count >= string.length + string.ljust(ljust_count, pad_number.to_s) + else + string[0..(ljust_count - 1)] end - # rubocop:enable Metrics/AbcSize, Metrics/MethodLength + end + # rubocop:enable Metrics/AbcSize, Metrics/MethodLength - def percentage_of(number) - return 0 if zero? || number.zero? + def percentage_of(number) + return 0 if zero? || number.zero? - (self / number.to_f) * 100.0 - end + (self / number.to_f) * 100.0 + end - def power(num) - self**num - end + def power(num) + self**num + end - def range(value) - (self - value)..(self + value) - end + def range(value) + (self - value)..(self + value) + end - def root(num) - self**(1.0 / num) - end + def root(num) + self**(1.0 / num) + end - def round_down(num = 0) - int, dec = to_f.to_s.split('.') + def round_down(num = 0) + int, dec = to_f.to_s.split('.') - "#{int}.#{dec[0...num]}".to_f - end + "#{int}.#{dec[0...num]}".to_f + end - def subtract(num) - self - num - end + def subtract(num) + self - num + end - def to_currency(options = {}) - unit = options[:unit] || '$' + def to_currency(options = {}) + unit = options[:unit] || '$' - "#{unit}#{pad_precision(options.only(:precision))}" - end + "#{unit}#{pad_precision(options.only(:precision))}" + end - def to_nearest_value(values = []) - return self if values.length.zero? + def to_nearest_value(values = []) + return self if values.length.zero? - value = values.first - difference = (self - value).abs + value = values.first + difference = (self - value).abs - values.each do |val| - next unless (self - val).abs < difference + values.each do |val| + next unless (self - val).abs < difference - difference = (self - val).abs - value = val - end - - value + difference = (self - val).abs + value = val end - def to_percentage(options = {}) - unit = options[:unit] || '%' + value + end - "#{pad_precision(options.only(:precision))}#{unit}" - end + def to_percentage(options = {}) + unit = options[:unit] || '%' - def within?(number, epsilon = 0.01) - return number == self if epsilon.zero? + "#{pad_precision(options.only(:precision))}#{unit}" + end - alpha = to_f - beta = number.to_f + def within?(number, epsilon = 0.01) + return number == self if epsilon.zero? - if alpha.zero? || beta.zero? - (alpha - beta).abs < epsilon - else - (alpha / beta - 1).abs < epsilon - end + alpha = to_f + beta = number.to_f + + if alpha.zero? || beta.zero? + (alpha - beta).abs < epsilon + else + (alpha / beta - 1).abs < epsilon end + end - alias eq? equal_to? - alias gt? greater_than? - alias gteq? greater_than_or_equal_to? - alias inequal_to? not_equal_to? - alias ineq? not_equal_to? - alias lt? less_than? - alias lteq? less_than_or_equal_to? - alias not_eq? not_equal_to? - alias plus_minus range + alias eq? equal_to? + alias gt? greater_than? + alias gteq? greater_than_or_equal_to? + alias inequal_to? not_equal_to? + alias ineq? not_equal_to? + alias lt? less_than? + alias lteq? less_than_or_equal_to? + alias not_eq? not_equal_to? + alias plus_minus range - end end