Class: Numeric

Inherits:
Object show all
Defined in:
lib/ramaze/snippets/numeric/filesize_format.rb,
lib/vendor/bacon.rb

Overview

Copyright © 2008 Michael Fellinger [email protected] All files in this distribution are subject to the terms of the Ruby license.

Constant Summary collapse

FILESIZE_FORMAT =
[
  ['%.1fT', 1 << 40],
  ['%.1fG', 1 << 30],
  ['%.1fM', 1 << 20],
  ['%.1fK', 1 << 10],
]

Instance Method Summary collapse

Instance Method Details

#close?(to, delta) ⇒ Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/vendor/bacon.rb', line 238

def close?(to, delta)
  (to.to_f - self).abs <= delta.to_f  rescue false
end

#filesize_formatObject

Output this number as easily readable filesize. Usage:

100_000.filesize_format             # => "97.7K"
100_000_000.filesize_format         # => "95.4M"
100_000_000_000.filesize_format     # => "93.1G"
100_000_000_000_000.filesize_format # => "90.9T"


18
19
20
21
22
23
24
# File 'lib/ramaze/snippets/numeric/filesize_format.rb', line 18

def filesize_format
  FILESIZE_FORMAT.each do |format, size|
    return format % (self.to_f / size) if self >= size
  end

  self.to_s
end