Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/utiles_string.rb
Overview
Extentions for String class.
Instance Method Summary collapse
-
#is_blank? ⇒ Boolean
Checks if it is blank.
-
#noe? ⇒ Boolean
Checks if it is nil or empty.
-
#to_currency ⇒ String
Formats string to currency.
-
#to_data_vol ⇒ String
Formats string to data volume.
Instance Method Details
#is_blank? ⇒ Boolean
Checks if it is blank.
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.
7 8 9 |
# File 'lib/utiles_string.rb', line 7 def noe? empty? end |
#to_currency ⇒ String
Formats string to currency.
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_vol ⇒ String
Formats string to data volume.
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 |