Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/more_ruby/string.rb
Instance Method Summary collapse
- #append(s) ⇒ Object
- #camelcase ⇒ Object
-
#camelcase_to_snakecase ⇒ Object
Converts aStringInCamelCase to a_string_in_camel_case.
- #capitalize_all ⇒ Object
-
#capitalize_first_letter_only ⇒ Object
.capitalize will upcase the first letter and lowercase everything else; this method does only the upcase part.
-
#escape ⇒ Object
Returns new string, does not modify in place.
-
#escape_whitespace ⇒ Object
Within the string, whitespace chars (n, r, t) are replaced by their text equivalents, e.g.
-
#extract_values_from_xml_string ⇒ Object
Take the XML-string and remove from it anything that looks like an XML tag.
- #formatted_number(separator = ",", count_limit = 3) ⇒ Object
-
#index_of_last_capital ⇒ Object
Returns the index of the last capital in the string (if one exists).
- #invert_case ⇒ Object
-
#is_hex? ⇒ Boolean
Does the string look like hex? !! will convrt nil to false, and anythig else (e.g. 0) to true.
-
#is_integer? ⇒ Boolean
Does the string look like an integer? !! will convrt nil to false, and anythig else (e.g. 0) to true.
- #join(s = "") ⇒ Object
-
#pascalcase ⇒ Object
Like camelcase but with the first letter also capitalised Pascalcase is C#‘s case convention.
-
#prefix_lines(prefix) ⇒ Object
Prefixes each line with the supplied string.
-
#random_case ⇒ Object
Returns a new string with each character’s case randomised.
- #snakecase ⇒ Object
- #snakecase_and_downcase ⇒ Object
- #to_bool ⇒ Object
-
#unindent ⇒ Object
Remove leading whitespace from each line in the string, using the indentation of the first line as the guide.
Instance Method Details
#append(s) ⇒ Object
105 106 107 |
# File 'lib/more_ruby/string.rb', line 105 def append(s) self << s end |
#camelcase ⇒ Object
76 77 78 79 |
# File 'lib/more_ruby/string.rb', line 76 def camelcase s = self.pascalcase s[0].downcase + s[1 .. -1] end |
#camelcase_to_snakecase ⇒ Object
Converts aStringInCamelCase to a_string_in_camel_case
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/more_ruby/string.rb', line 82 def camelcase_to_snakecase a = [] self =~ /(^[a-z]+)/ a << $1 if $1 m = self.scan /([A-Z]+[a-z0-9]*)/ m.flatten! m.each do |fragment| fragment =~ /([A-Z]+[a-z0-9]*)/ caps = $1 lower = $2 if caps.size > 1 s = "_#{caps.downcase}" a << s caps = "" # set to empty string so that the below caps.downcase still works end s = "_#{caps.downcase}#{lower}" a << s unless s == "_" end a.join end |
#capitalize_all ⇒ Object
24 25 26 |
# File 'lib/more_ruby/string.rb', line 24 def capitalize_all downcase.scan(/[a-z0-9]+/).map { |z| z.capitalize }.join(' ') end |
#capitalize_first_letter_only ⇒ Object
.capitalize will upcase the first letter and lowercase everything else; this method does only the upcase part
5 6 7 8 |
# File 'lib/more_ruby/string.rb', line 5 def capitalize_first_letter_only return self if empty? return self[0].upcase + self[1 .. -1] # self[1 .. -1] will return "" if self is only one character long end |
#escape ⇒ Object
Returns new string, does not modify in place
11 12 13 |
# File 'lib/more_ruby/string.rb', line 11 def escape Regexp.escape(self) end |
#escape_whitespace ⇒ Object
Within the string, whitespace chars (n, r, t) are replaced by their text equivalents, e.g. n => \n Does not modify self
165 166 167 168 169 170 171 |
# File 'lib/more_ruby/string.rb', line 165 def escape_whitespace copy = self.dup copy.gsub!("\n", "\\n") copy.gsub!("\r", "\\r") copy.gsub!("\t", "\\t") copy end |
#extract_values_from_xml_string ⇒ Object
Take the XML-string and remove from it anything that looks like an XML tag
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/more_ruby/string.rb', line 149 def extract_values_from_xml_string values = [] a = self.split("<").collect { |z| z.chomp } # need to remove whitespace where possible # Now remove from the array any items that end in a > - these items look like fragments of tags a.delete_if { |z| z =~ />$/ } # a might now contain nils, "" and items like "foo>bar" a.delete_if { |z| z == "" } a.compact! a.each { |z| values << z.split(">")[1] } values end |
#formatted_number(separator = ",", count_limit = 3) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/more_ruby/string.rb', line 131 def formatted_number(separator = ",", count_limit = 3) copy = self.dup s = "" count = 0 while copy.size > 0 if count == count_limit s << separator count = 0 else count += 1 s << copy[-1] copy.chop! end end s.reverse end |
#index_of_last_capital ⇒ Object
Returns the index of the last capital in the string (if one exists)
55 56 57 58 59 60 |
# File 'lib/more_ruby/string.rb', line 55 def index_of_last_capital pos = 0 reverse_index = self.reverse.index /[A-Z]/ return nil if reverse_index == nil self.size - reverse_index - 1 end |
#invert_case ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/more_ruby/string.rb', line 28 def invert_case s = "" self.chars do |c| if c =~ /[A-Z]/ s << c.downcase elsif c =~ /[a-z]/ s << c.upcase else s << c end end raise "invert_case failed!" unless s.size == self.size s end |
#is_hex? ⇒ Boolean
Does the string look like hex? !! will convrt nil to false, and anythig else (e.g. 0) to true
127 128 129 |
# File 'lib/more_ruby/string.rb', line 127 def is_hex? !!(self =~ /^[0-9a-fA-F]+$/) end |
#is_integer? ⇒ Boolean
Does the string look like an integer? !! will convrt nil to false, and anythig else (e.g. 0) to true
121 122 123 |
# File 'lib/more_ruby/string.rb', line 121 def is_integer? !!(self =~ /^[0-9]+$/) end |
#join(s = "") ⇒ Object
109 110 111 |
# File 'lib/more_ruby/string.rb', line 109 def join(s = "") append(s) end |
#pascalcase ⇒ Object
Like camelcase but with the first letter also capitalised Pascalcase is C#‘s case convention
72 73 74 |
# File 'lib/more_ruby/string.rb', line 72 def pascalcase downcase.scan(/[a-z0-9]+/).map { |x| x.capitalize }.join end |
#prefix_lines(prefix) ⇒ Object
Prefixes each line with the supplied string
44 45 46 |
# File 'lib/more_ruby/string.rb', line 44 def prefix_lines(prefix) gsub(/^/) { prefix } end |
#random_case ⇒ Object
Returns a new string with each character’s case randomised
16 17 18 19 20 21 22 |
# File 'lib/more_ruby/string.rb', line 16 def random_case new = "" self.each_char do |x| new << ((rand(2) % 2 == 0) ? x.upcase : x.downcase) end new end |
#snakecase ⇒ Object
62 63 64 |
# File 'lib/more_ruby/string.rb', line 62 def snakecase gsub(/[^a-zA-Z0-9]+/, '_') end |
#snakecase_and_downcase ⇒ Object
66 67 68 |
# File 'lib/more_ruby/string.rb', line 66 def snakecase_and_downcase downcase.snakecase end |
#to_bool ⇒ Object
113 114 115 116 117 |
# File 'lib/more_ruby/string.rb', line 113 def to_bool return true if self.downcase == "true" return false if self.downcase == "false" raise "String #{self} was neither 'true' nor 'false'. Cannot convert to boolean in this custom method." end |
#unindent ⇒ Object
Remove leading whitespace from each line in the string, using the indentation of the first line as the guide
49 50 51 52 |
# File 'lib/more_ruby/string.rb', line 49 def unindent leading_whitespace = self[/\A\s*/] self.gsub(/^#{leading_whitespace}/, '') end |