Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/lafcadio/util.rb
Instance Method Summary collapse
-
#countOccurrences(regexp) ⇒ Object
Returns the number of times that
regexp
occurs in the string. -
#decapitalize ⇒ Object
Decapitalizes the first letter of the string, or decapitalizes the entire string if it’s all capitals.
-
#incrementFilename ⇒ Object
Increments a filename.
-
#lineWrap(lineLength) ⇒ Object
Breaks a string into lines no longer than
lineLength
. -
#numericStringToUsFormat ⇒ Object
Turns a numeric string into U.S.
-
#pad(size, fillChar) ⇒ Object
Left-pads a string with
fillChar
up tosize
size. -
#splitKeepInBetweens(regexp) ⇒ Object
Divides a string into substrings using
regexp
as a delimiter, and returns an array containing both the substrings and the portions that matchedregexp
.
Instance Method Details
#countOccurrences(regexp) ⇒ Object
Returns the number of times that regexp
occurs in the string.
72 73 74 75 76 77 78 79 80 |
# File 'lib/lafcadio/util.rb', line 72 def countOccurrences(regexp) count = 0 str = self.clone while str =~ regexp count += 1 str = $' end count end |
#decapitalize ⇒ Object
Decapitalizes the first letter of the string, or decapitalizes the entire string if it’s all capitals.
'InternalClient'.decapitalize -> "internalClient"
'SKU'.decapitalize -> "sku"
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lafcadio/util.rb', line 87 def decapitalize string = clone firstLetter = string[0..0].downcase string = firstLetter + string[1..string.length] newString = "" while string =~ /([A-Z])([^a-z]|$)/ newString += $` newString += $1.downcase string = $2 + $' end newString += string newString end |
#incrementFilename ⇒ Object
Increments a filename. If the filename ends with a number, it increments that number; otherwise it appends a “_1” after the filename but before the file extension.
"john.jpg".incrementFilename -> "john_1.jpg"
"john_1.jpg".incrementFilename -> "john_2.jpg"
"john_2.jpg".incrementFilename -> "john_3.jpg"
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/lafcadio/util.rb', line 108 def incrementFilename filename = self.clone extension = filename.split(/\./).last filename.sub!(/\..*$/, '') if filename =~ /_(\d*)$/ newSuffix = $1.to_i + 1 filename = $` + "_#{newSuffix}" else filename += "_1" end filename += ".#{extension}" filename end |
#lineWrap(lineLength) ⇒ Object
Breaks a string into lines no longer than lineLength
.
'the quick brown fox jumped over the lazy dog.'.lineWrap( 10 ) ->
"the quick\nbrown fox\njumped\nover the\nlazy dog."
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/lafcadio/util.rb', line 126 def lineWrap(lineLength) words = split ' ' line = '' lines = [] words.each { |word| if line.length + word.length + 1 > lineLength lines << line line = '' end line = line != '' ? "#{ line } #{ word }" : word } lines << line lines.join "\n" end |
#numericStringToUsFormat ⇒ Object
Turns a numeric string into U.S. format if it’s not already formatted that way.
"10,00".numericStringToUsFormat -> "10.00"
"10.00".numericStringToUsFormat -> "10.00"
146 147 148 149 150 |
# File 'lib/lafcadio/util.rb', line 146 def numericStringToUsFormat numericString = clone numericString.gsub!(/,/, '.') if numericString =~ /,\d{2}$/ numericString end |
#pad(size, fillChar) ⇒ Object
Left-pads a string with fillChar
up to size
size.
"a".pad( 10, "+") -> "+++++++++a"
155 156 157 158 159 160 161 |
# File 'lib/lafcadio/util.rb', line 155 def pad(size, fillChar) string = clone while string.length < size string = fillChar + string end string end |
#splitKeepInBetweens(regexp) ⇒ Object
Divides a string into substrings using regexp
as a delimiter, and returns an array containing both the substrings and the portions that matched regexp
.
'theZquickZZbrownZfox'.splitKeepInBetweens(/Z+/) ->
['the', 'Z', 'quick', 'ZZ', 'brown', 'Z', 'fox' ]
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lafcadio/util.rb', line 169 def splitKeepInBetweens(regexp) result = [] string = clone while string =~ regexp result << $` result << $& string = $' end result << string unless string == '' result end |