Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/utilities.rb

Instance Method Summary collapse

Instance Method Details

#degreesObject

Convert to degrees



55
56
57
# File 'lib/utilities.rb', line 55

def degrees
  self * Math::PI / 180
end

#hour_to_string(delimiter = ':') ⇒ Object

Transform self to a string formatted time (HH:MM) Ex: 14.5 => “14:30“



65
66
67
68
69
# File 'lib/utilities.rb', line 65

def hour_to_string delimiter = ':'
  hour  = self.to_i
  min   = "%02d" % (self.abs * 60 % 60).to_i
  "#{hour}#{delimiter}#{min}"
end

#percentage_of(n, t = 100) ⇒ Object Also known as: percent_of

Calculate the percentage of self on n



88
89
90
# File 'lib/utilities.rb', line 88

def percentage_of n, t = 100
  n == 0 ? 0.0 : self / n.to_f * t
end

#rank(min, max) ⇒ Object

Calculate the rank of self based on provided min and max



77
78
79
80
# File 'lib/utilities.rb', line 77

def rank min, max
  s, min, max = self.to_f, min.to_f, max.to_f
  min == max ? 0.0 : (s - min) / (max - min)
end

#sqrtObject

Return the square root of self



72
73
74
# File 'lib/utilities.rb', line 72

def sqrt
  Math.sqrt(self)
end

#squareObject

Return the square of self



60
61
62
# File 'lib/utilities.rb', line 60

def square
  self * self
end

#to_decimals(decimals = 2) ⇒ Object

Transforms self to a string with decimals decimals



83
84
85
# File 'lib/utilities.rb', line 83

def to_decimals decimals = 2
  "%.#{decimals}f" % self
end