Class: Numeric

Inherits:
Object show all
Defined in:
lib/roebe/core/numeric.rb

Overview

#

Numeric

The parent class for Integer and Float in Ruby.

#

Instance Method Summary collapse

Instance Method Details

#factorialObject

#

factorial

5.factorial

#


58
59
60
61
62
# File 'lib/roebe/core/numeric.rb', line 58

def factorial
  f = 1
  self.downto(2) { |x| f *= x }
  f
end

#to_human_readable_bytes(optional_padding = ' ') ⇒ Object

#

to_human_readable_bytes

Warning: This may be not 100% correct for very large values (like TB) because Math.log uses floating point arithmetic.

Nevertheless, usage examples come next:

1000000000.to_human_readable_bytes # => "953.674MB
1612695528.to_human_readable_bytes
123.452.to_human_readable_bytes
File.size?(ENV['RUBY_WWW']+'/cybersprawl/aggregate_urls.rb').to_human_readable_bytes
267669.to_human_readable_bytes # => "261.396 KB"
#


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/roebe/core/numeric.rb', line 22

def to_human_readable_bytes(
    optional_padding = ' '
  )
  units = [ ' B','KB','MB','GB','TB' ] # should be two chars for better alignment
  begin
    if self == 0
      e = 0
    else
      e = ( Math.log(self) / Math.log(1024) ).floor
    end
    s = '%.3f' % (to_f / 1024 ** e)
    # if optional_pad_behind_dot
    #   if s.to_s.split('.').last.size < 3
    #     s = s.to_s + '0'
    #   end
    # end
    # ===================================================================== #
    # Insert proper units next:
    # ===================================================================== #
    s.sub(/\.?0*$/, optional_padding+units[e])
  rescue Exception => e # silent rescue.
  end
end

#to_radObject

#

to_rad

#


49
50
51
# File 'lib/roebe/core/numeric.rb', line 49

def to_rad
  self / 180.0 * Math::PI
end