Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/fat_core/string.rb
Class Method Summary collapse
Instance Method Summary collapse
- #as_string ⇒ Object
-
#as_sym ⇒ Object
Convert to symbol “Hello World” -> :hello_world.
- #colorize(text, color_code) ⇒ Object
- #console_blink ⇒ Object
- #console_blue ⇒ Object
- #console_bold ⇒ Object
- #console_dark_blue ⇒ Object
- #console_dark_green ⇒ Object
- #console_dark_red ⇒ Object
- #console_dark_yellow ⇒ Object
- #console_def ⇒ Object
- #console_green ⇒ Object
- #console_purple ⇒ Object
-
#console_red ⇒ Object
Thanks to Eugene at stackoverflow for the following.
- #console_yellow ⇒ Object
-
#digdate2iso ⇒ Object
Convert a string with an all-digit date to an iso string E.g., “20090923” -> “2009-09-23”.
- #distance(other, block_size: 1, max_distance: 10) ⇒ Object
- #entitle ⇒ Object
- #entitle! ⇒ Object
-
#fuzzy_match(other) ⇒ Object
See if self contains colon- or space-separated words that include the colon- or space-separated words of other.
-
#matches_with(str) ⇒ Object
Here are instance methods for the class that includes Matchable This tries to convert the receiver object into a string, then matches against the given matcher, either via regex or a fuzzy string matcher.
- #tex_quote ⇒ Object
-
#to_regexp ⇒ Object
Convert a string of the form ‘/…/Iixm’ to a regular expression.
- #wrap(width = 70, hang = 0) ⇒ Object
Class Method Details
.random(size = 8) ⇒ Object
117 118 119 |
# File 'lib/fat_core/string.rb', line 117 def self.random(size = 8) ('a'..'z').cycle.take(size).shuffle.join end |
Instance Method Details
#as_string ⇒ Object
80 81 82 |
# File 'lib/fat_core/string.rb', line 80 def as_string self end |
#as_sym ⇒ Object
Convert to symbol “Hello World” -> :hello_world
75 76 77 78 |
# File 'lib/fat_core/string.rb', line 75 def as_sym strip.squeeze(' ').gsub(/\s+/, '_'). gsub(/[^_A-Za-z0-9]/, '').downcase.to_sym end |
#colorize(text, color_code) ⇒ Object
198 |
# File 'lib/fat_core/string.rb', line 198 def colorize(text, color_code) "#{color_code}#{text}\001\e[0m\002" end |
#console_blink ⇒ Object
196 |
# File 'lib/fat_core/string.rb', line 196 def console_blink; colorize(self, "\001\e[5m\002"); end |
#console_blue ⇒ Object
190 |
# File 'lib/fat_core/string.rb', line 190 def console_blue; colorize(self, "\001\e[1m\e[34m\002"); end |
#console_bold ⇒ Object
195 |
# File 'lib/fat_core/string.rb', line 195 def console_bold; colorize(self, "\001\e[1m\002"); end |
#console_dark_blue ⇒ Object
191 |
# File 'lib/fat_core/string.rb', line 191 def console_dark_blue; colorize(self, "\001\e[34m\002"); end |
#console_dark_green ⇒ Object
187 |
# File 'lib/fat_core/string.rb', line 187 def console_dark_green; colorize(self, "\001\e[32m\002"); end |
#console_dark_red ⇒ Object
185 |
# File 'lib/fat_core/string.rb', line 185 def console_dark_red; colorize(self, "\001\e[31m\002"); end |
#console_dark_yellow ⇒ Object
189 |
# File 'lib/fat_core/string.rb', line 189 def console_dark_yellow; colorize(self, "\001\e[33m\002"); end |
#console_def ⇒ Object
194 |
# File 'lib/fat_core/string.rb', line 194 def console_def; colorize(self, "\001\e[1m\002"); end |
#console_green ⇒ Object
186 |
# File 'lib/fat_core/string.rb', line 186 def console_green; colorize(self, "\001\e[1m\e[32m\002"); end |
#console_purple ⇒ Object
192 |
# File 'lib/fat_core/string.rb', line 192 def console_purple; colorize(self, "\001\e[1m\e[35m\002"); end |
#console_red ⇒ Object
Thanks to Eugene at stackoverflow for the following. stackoverflow.com/questions/8806643/
colorized-output-breaks-linewrapping-with-readline
These color strings without confusing readline about the length of the prompt string in the shell. (Unlike the rainbow routines)
184 |
# File 'lib/fat_core/string.rb', line 184 def console_red; colorize(self, "\001\e[1m\e[31m\002"); end |
#console_yellow ⇒ Object
188 |
# File 'lib/fat_core/string.rb', line 188 def console_yellow; colorize(self, "\001\e[1m\e[33m\002"); end |
#digdate2iso ⇒ Object
Convert a string with an all-digit date to an iso string E.g., “20090923” -> “2009-09-23”
123 124 125 |
# File 'lib/fat_core/string.rb', line 123 def digdate2iso self.sub(/(\d\d\d\d)(\d\d)(\d\d)/, '\1-\2-\3') end |
#distance(other, block_size: 1, max_distance: 10) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/fat_core/string.rb', line 4 def distance(other, block_size: 1, max_distance: 10) dl = DamerauLevenshtein # NOTE: DL 'gives up after' max_distance, so the distance function # will return max_distance+1 if the distance is bigger than that. # Here we subtract 1 so the max_distance also becomes the max # return value. dl.distance(self, other, block_size, max_distance-1) end |
#entitle ⇒ Object
175 176 177 |
# File 'lib/fat_core/string.rb', line 175 def entitle self.dup.entitle! end |
#entitle! ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/fat_core/string.rb', line 127 def entitle! little_words = %w[ a an the and or in on under of from as by to ] newwords = [] words = split(/\s+/) first_word = true num_words = words.length words.each_with_index do |w, k| last_word = (k + 1 == num_words) if w =~ %r[c/o]i # Care of newwords.push("c/o") elsif w =~ %r[^p\.?o\.?$]i # Post office newwords.push("P.O.") elsif w =~ %r[^[0-9]+(st|nd|rd|th)$]i # Ordinals newwords.push(w.downcase) elsif w =~ %r[^(cr|dr|st|rd|ave|pk|cir)$]i # Common abbrs to capitalize newwords.push(w.capitalize) elsif w =~ %r[^(us|ne|se|rr)$]i # Common 2-letter abbrs to upcase newwords.push(w.upcase) elsif w =~ %r[^[0-9].*$]i # Other runs starting with numbers, # like 3-A newwords.push(w.upcase) elsif w =~ %r[^(N|S|E|W|NE|NW|SE|SW)$]i # Compass directions all caps newwords.push(w.upcase) elsif w =~ %r[^[^aeiouy]*$]i && w.size > 2 # All consonants and at least 3 chars, probably abbr newwords.push(w.upcase) elsif w =~ %r[^(\w+)-(\w+)$]i # Hypenated double word newwords.push($1.capitalize + '-' + $2.capitalize) elsif little_words.include?(w.downcase) # Only capitalize at beginning or end newwords.push((first_word or last_word) ? w.capitalize : w.downcase) else # All else newwords.push(w.capitalize) end first_word = false end self[0..-1] = newwords.join(' ') end |
#fuzzy_match(other) ⇒ Object
See if self contains colon- or space-separated words that include the colon- or space-separated words of other. Return the matched portion of self. Other cannot be a regex embedded in a string.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fat_core/string.rb', line 16 def fuzzy_match(other) # Remove periods, commas, and apostrophes other = other.gsub(/[\*.,']/, '') target = self.gsub(/[\*.,']/, '') matched_text = nil matchers = other.split(/[: ]+/) regexp_string = matchers.map {|m| ".*?#{Regexp.escape(m)}.*?"}.join('[: ]') regexp_string.sub!(/^\.\*\?/, '') regexp_string.sub!(/\.\*\?$/, '') regexp = /#{regexp_string}/i if match = regexp.match(target) matched_text = match[0] else matched_text = nil end matched_text end |
#matches_with(str) ⇒ Object
Here are instance methods for the class that includes Matchable This tries to convert the receiver object into a string, then matches against the given matcher, either via regex or a fuzzy string matcher.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fat_core/string.rb', line 38 def matches_with(str) if str.nil? nil elsif str =~ /^\s*\// re = str.to_regexp if self.to_s =~ re $& else nil end else self.to_s.fuzzy_match(str) end end |
#tex_quote ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fat_core/string.rb', line 102 def tex_quote r = self.dup r = r.gsub(/[{]/, 'XzXzXobXzXzX') r = r.gsub(/[}]/, 'XzXzXcbXzXzX') r = r.gsub(/\\/, '\textbackslash{}') r = r.gsub(/\^/, '\textasciicircum{}') r = r.gsub(/~/, '\textasciitilde{}') r = r.gsub(/\|/, '\textbar{}') r = r.gsub(/\</, '\textless{}') r = r.gsub(/\>/, '\textgreater{}') r = r.gsub(/([_$&%#])/) { |m| '\\' + m } r = r.gsub('XzXzXobXzXzX', '\\{') r = r.gsub('XzXzXcbXzXzX', '\\}') end |
#to_regexp ⇒ Object
Convert a string of the form ‘/…/Iixm’ to a regular expression. However, make the regular expression case-insensitive by default and extend the modifier syntax to allow ‘/I’ to indicate case-sensitive.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fat_core/string.rb', line 56 def to_regexp if self =~ /^\s*\/([^\/]*)\/([Iixm]*)\s*$/ body = $1 opts = $2 flags = Regexp::IGNORECASE unless opts.blank? flags = 0 if opts.include?('I') flags |= Regexp::IGNORECASE if opts.include?('i') flags |= Regexp::EXTENDED if opts.include?('x') flags |= Regexp::MULTILINE if opts.include?('m') end flags = nil if flags == 0 Regexp.new(body, flags) else Regexp.new(self) end end |
#wrap(width = 70, hang = 0) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fat_core/string.rb', line 84 def wrap(width=70, hang=0) offset = 0 trip = 1 result = '' while (s = slice(offset, width)) offset += width if trip == 1 width -= hang else s = (' ' * hang) + s end result << s + "\n" trip += 1 end # Remove the final newline before exiting result.strip end |