Module: StringHelper
- Included in:
- String
- Defined in:
- lib/rubyhelper/string.rb
Instance Method Summary collapse
-
#^(k) ⇒ String
CRYXOR (one time pad dirt application).
-
#false? ⇒ true or false
indicate if the string is “false”.
-
#p(replace = '') ⇒ String
Remove accents from the string, and replace it by the same letter in ASCII Note : it doesn’t remove non ASCII characters.
-
#p!(replace = '') ⇒ String
see #p.
-
#scapitalize ⇒ String
Capitalize a sequence (each word).
-
#scapitalize! ⇒ String
see #scapitalize.
-
#sha2 ⇒ String
SHA2 shortcuts see Digest::SHA2#hexdigest.
-
#sha2! ⇒ String
see #sha2.
-
#splity(sep = "\n") ⇒ Array
by [email protected] split th string and only keep the non empty striped values.
-
#splity!(sep) ⇒ Array
see #splity.
-
#static(n, char = ' ', place = :right) ⇒ String
Get a str with a static length.
-
#static!(n, char = ' ') ⇒ Object
see #static.
-
#to_ascii(replace = '', case_mod = nil) ⇒ String
Return a simple ascii string.
-
#to_case(case_mod = :downcase) ⇒ String
permit to do upcase/downcase/capitalize easier with a simple param.
-
#to_case!(case_mod = :downcase) ⇒ String
see #to_case.
-
#to_plain(case_mod = nil, replace = '') ⇒ String
UTF-8 encoding and replace invalid chars.
-
#to_plain!(case_mod = nil, replace = '') ⇒ String
see #to_plain.
-
#to_t ⇒ true or false or nil
Returns true or false if the string if “true” or “false”.
-
#true? ⇒ true or false
indicate if the string is “true”.
-
#utf8(replace = '') ⇒ String
TODO : raise error on invalid utf-8 param replace Force utf-8 encoding (shortcut ;) ! ).
-
#utf8! ⇒ String
see #utf8.
Instance Method Details
#^(k) ⇒ String
CRYXOR (one time pad dirt application)
117 118 119 120 121 122 123 124 |
# File 'lib/rubyhelper/string.rb', line 117 def ^(k) raise ArgumentError, "The key MUST BE a String" unless key.is_a? String str = "" self.size.times do |i| str << (self[i].ord ^ k[i % k.size].ord).chr end return str end |
#false? ⇒ true or false
indicate if the string is “false”
212 213 214 |
# File 'lib/rubyhelper/string.rb', line 212 def false? return (self == "false") end |
#p(replace = '') ⇒ String
Remove accents from the string, and replace it by the same letter in ASCII Note : it doesn’t remove non ASCII characters
53 54 55 56 57 58 59 60 |
# File 'lib/rubyhelper/string.rb', line 53 def p(replace='') begin return self.tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz") rescue Encoding::CompatibilityError => e return self.utf8(replace) end end |
#p!(replace = '') ⇒ String
see #p
66 67 68 |
# File 'lib/rubyhelper/string.rb', line 66 def p!(replace='') return self.replace(self.p(replace)) end |
#scapitalize ⇒ String
Capitalize a sequence (each word)
219 220 221 |
# File 'lib/rubyhelper/string.rb', line 219 def scapitalize return self.split.map(&:capitalize).join(' ') end |
#scapitalize! ⇒ String
see #scapitalize
226 227 228 |
# File 'lib/rubyhelper/string.rb', line 226 def scapitalize! return self.replace(self.scapitalize) end |
#sha2 ⇒ String
SHA2 shortcuts see Digest::SHA2#hexdigest
130 131 132 |
# File 'lib/rubyhelper/string.rb', line 130 def sha2 Digest::SHA2.hexdigest(self) end |
#sha2! ⇒ String
see #sha2
137 138 139 |
# File 'lib/rubyhelper/string.rb', line 137 def sha2! return self.replace(self.sha2) end |
#splity(sep = "\n") ⇒ Array
by [email protected] split th string and only keep the non empty striped values
236 237 238 239 |
# File 'lib/rubyhelper/string.rb', line 236 def splity(sep = "\n") raise ArgumentError, "sep must be a string or a regex" unless sep.is_a? String or sep.is_a? Regexp return self.split(sep).map{|e| ((e.strip.empty?) ? (nil) : (e.strip))}.compact end |
#splity!(sep) ⇒ Array
see #splity
245 246 247 |
# File 'lib/rubyhelper/string.rb', line 245 def splity!(sep) return self.replace(self.splity(sep)) end |
#static(n, char = ' ', place = :right) ⇒ String
Get a str with a static length. If the str size > n, reduce the str (keep str from the (param place) ) You should check the test files for examples if the (param place) is not valid, the function will just return self Note : #center #ljust and #rjust do a similar work.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/rubyhelper/string.rb', line 152 def static(n, char =' ', place = :right) raise ArgumentError, 'char is not an Char (String)' unless char.is_a? String raise ArgumentError, 'n is not an Integer' unless n.is_a? Integer char = char[0] || " " # get only the first char or a space if empty if size < n case place when :begin, :front, :left return char * (n - size).to_i + self when :center, :middle return char * ((n - size) / 2) + self + char * ((n - size) / 2 + (n - size) % 2) when :end, :back, :right return self + char * (n - size).to_i else return self end else case place when :begin, :front, :left return self[0...n] when :center, :middle return self[((-(size() +n - 1)) / 2)..((-(size() -n + 1)) / 2)] when :end, :back, :right return self[(-n)..(-1)] else return self end end end |
#static!(n, char = ' ') ⇒ Object
see #static
184 185 186 |
# File 'lib/rubyhelper/string.rb', line 184 def static!(n, char=' ') return self.replace(self.static(n, char)) end |
#to_ascii(replace = '', case_mod = nil) ⇒ String
Return a simple ascii string. Invalid characters will be replaced by “replace” (argument) Accents are removed first and replaced by the equivalent ASCII letter (example : ‘é’ => ‘e’) no raise error on #p because of default doesn’t let it happen ;)
104 105 106 107 108 109 110 111 |
# File 'lib/rubyhelper/string.rb', line 104 def to_ascii(replace='', case_mod = nil) raise ArgumentError, "Argument replace is not a String char" unless replace.is_a? String s = String.new self.p.each_char do |c| s += ((c.ord > 255) ? (replace) : (c)) end return s.to_case(case_mod) end |
#to_case(case_mod = :downcase) ⇒ String
permit to do upcase/downcase/capitalize easier with a simple param
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rubyhelper/string.rb', line 74 def to_case(case_mod = :downcase) case case_mod when :upcase return self.upcase when :downcase return self.downcase when :capitalize return self.scapitalize when :classic return self.capitalize else return self end end |
#to_case!(case_mod = :downcase) ⇒ String
see #to_case
92 93 94 |
# File 'lib/rubyhelper/string.rb', line 92 def to_case!(case_mod = :downcase) return self.replace(self.to_case(case_mod)) end |
#to_plain(case_mod = nil, replace = '') ⇒ String
UTF-8 encoding and replace invalid chars. Remove accents from the string (convert to ASCII chars !) And then, change the case as first argument if not nil
35 36 37 |
# File 'lib/rubyhelper/string.rb', line 35 def to_plain(case_mod = nil, replace='') return self.p(replace).utf8(replace).to_case(case_mod) end |
#to_plain!(case_mod = nil, replace = '') ⇒ String
see #to_plain
43 44 45 |
# File 'lib/rubyhelper/string.rb', line 43 def to_plain!(case_mod = nil, replace='') return self.replace(self.to_plain(case_mod, replace)) end |
#to_t ⇒ true or false or nil
Returns true or false if the string if “true” or “false”. else nil
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rubyhelper/string.rb', line 191 def to_t case self when "true" return true when "false" return false else return nil end end |
#true? ⇒ true or false
indicate if the string is “true”
205 206 207 |
# File 'lib/rubyhelper/string.rb', line 205 def true? return (self == "true") end |
#utf8(replace = '') ⇒ String
TODO : raise error on invalid utf-8 param replace Force utf-8 encoding (shortcut ;) ! )
14 15 16 17 |
# File 'lib/rubyhelper/string.rb', line 14 def utf8 replace='' raise ArgumentError, 'replace is not a valid char (String)' unless replace.is_a? String return self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: replace) end |