Class: String

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

Overview

Extentions for String class.

Instance Method Summary collapse

Instance Method Details

#is_blank?Boolean

Checks if it is blank.

Returns:

  • (Boolean)


13
14
15
# File 'lib/utiles_string.rb', line 13

def is_blank?
  noe? or self =~ /^\s+$/
end

#noe?Boolean

Checks if it is nil or empty.

Returns:

  • (Boolean)


7
8
9
# File 'lib/utiles_string.rb', line 7

def noe?
  empty?
end

#to_currencyString

Formats string to currency.

Examples:

"0202".to_currency => "2,02 zł"

Returns:



22
23
24
25
26
27
# File 'lib/utiles_string.rb', line 22

def to_currency
  zl = to_i/100
  gr = to_i%100
  gr = "0#{gr}" if gr < 10
  "#{zl},#{gr} zł"
end

#to_data_volString

Formats string to data volume.

Examples:

"18539311".to_date_vol => "185 MB"
"18532".to_date_vol => "18,5 kB"

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/utiles_string.rb', line 35

def to_data_vol
  case
    when length > 9
      label = "GB"
      border = 9
    when length > 6
      label = "MB"
      border = 6
    when length > 3
      label = "kB"
      border = 3
    else
      label = "B"
      return self == "0" ? "0" : "#{self} #{label}"
  end
  int = self[0,length-border].to_i
  rest = self[-border,border]
  sets = int < 10 ? rest[0,2] : int < 100 ? rest[0,1] : ""
  setss = int < 100 ? sets.to_i.zero? ? "" : ",#{sets}" : ""
  "#{int}#{setss} #{label}"
end