Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/calc_profit/core_ext/string.rb
Instance Method Summary collapse
-
#clean ⇒ Object
Remove leading and trailing white space and compress internal runs of white space to a single space.
- #dequote ⇒ Object
-
#format_by(format) ⇒ Object
Format the string according to the given format.
Instance Method Details
#clean ⇒ Object
Remove leading and trailing white space and compress internal runs of white space to a single space.
12 13 14 |
# File 'lib/calc_profit/core_ext/string.rb', line 12 def clean self.strip.squeeze(' ') end |
#dequote ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/calc_profit/core_ext/string.rb', line 2 def dequote if self.strip =~ /^"/ self.sub(/^\s*"?([^"]*)"?\s*$/, '\1') else self end end |
#format_by(format) ⇒ Object
Format the string according to the given format. A format of n, where n is an integer, formats the string as a number with embedded commas and rounded to n decimal places. A nil format just returns the string.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/calc_profit/core_ext/string.rb', line 19 def format_by(format) case format when Integer val = gsub(/[$,]/, '') if val.number? val.to_f.commas(format) else self end else self end end |