Class: Fixnum

Inherits:
Object show all
Defined in:
lib/iron/extensions/fixnum.rb

Instance Method Summary collapse

Instance Method Details

#to_human_size(precision = 1) ⇒ Object

Adapted from Rails’ NumberHelper view helper



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/iron/extensions/fixnum.rb', line 4

def to_human_size(precision=1)
  size = Kernel.Float(self)
  case
  when size.to_i == 1 then
    "1 Byte"
  when size < 1024 then
    "#{size.to_i} Bytes"
  when size < 1024*1024 then
    "#{(size / 1024).to_display(precision)} KB"
  when size < 1024*1024*1024 then
    "#{(size / (1024*1024)).to_display(precision)} MB"
  when size < 1024*1024*1024*1024 then
    "#{(size / (1024*1024*1024)).to_display(precision)} GB"
  else
    "#{(size / (1024*1024*1024*1024)).to_display(precision)} TB"
  end
rescue
  nil
end