Module: StringHelper
- Included in:
- String
- Defined in:
- lib/rubyhelper/stringhelper.rb
Instance Method Summary collapse
-
#^(k) ⇒ Object
CRYXOR.
-
#get_float ⇒ Object
as get_int but with .
- #get_float! ⇒ Object
-
#get_int ⇒ Object
get only the digits and symbols in the string.
- #get_int! ⇒ Object
-
#p ⇒ Object
Remove accents.
- #p!(replace = " ") ⇒ Object
-
#scapitalize ⇒ Object
Capitalize a sequence.
- #scapitalize! ⇒ Object
-
#sha2 ⇒ Object
SHA2.
- #sha2! ⇒ Object
-
#static(n, char = ' ') ⇒ Object
Param n: number of char char: char to replace if the initial str is too short Get a str with a static length.
- #static!(n, char = ' ') ⇒ Object
-
#to_ascii(replace = "", case_mod = nil) ⇒ Object
Params replace: a caracter to replace non-ascii chars case_mod: nil (not changement), :upcase, :capitalize or :downcase return a simple ascii string.
-
#to_case(case_mod = :downcase) ⇒ Object
Params case_mod: nil (not changement), :upcase, :capitalize or :downcase permit to do upcase/downcase/capitalize easier with a simple param.
- #to_case!(case_mod = :downcase) ⇒ Object
-
#to_f ⇒ Object
improvement of to_f to count “,” caracter as “.”.
-
#to_i_wd(char = ' ') ⇒ Object
To_i with delimiter.
-
#to_plain(case_mod = nil, replace = " ") ⇒ Object
Params case_mod: nil (not changement), :upcase, :capitalize or :downcase replace: if a char is not utf8 valid, character will replace it UTF-8 encoding and replace invalid chars, Remove accents from the string.
-
#to_t ⇒ Object
Returns true or false if the string if “true” or “false”.
Instance Method Details
#^(k) ⇒ Object
CRYXOR
71 72 73 74 75 76 77 |
# File 'lib/rubyhelper/stringhelper.rb', line 71 def ^(k) str = "" self.size.times do |i| str << (self[i].ord ^ k[i % k.size].ord).chr end return str end |
#get_float ⇒ Object
as get_int but with . and ,
123 124 125 |
# File 'lib/rubyhelper/stringhelper.rb', line 123 def get_float return self.gsub(/[^\d\.\,\-\+]/, "") end |
#get_float! ⇒ Object
126 127 128 |
# File 'lib/rubyhelper/stringhelper.rb', line 126 def get_float! return self.replace(self.get_float) end |
#get_int ⇒ Object
get only the digits and symbols in the string
115 116 117 |
# File 'lib/rubyhelper/stringhelper.rb', line 115 def get_int return self.gsub(/[^\d\-\+]/, "") end |
#get_int! ⇒ Object
118 119 120 |
# File 'lib/rubyhelper/stringhelper.rb', line 118 def get_int! return self.replace(self.get_int) end |
#p ⇒ Object
Remove accents
17 18 19 20 |
# File 'lib/rubyhelper/stringhelper.rb', line 17 def p() return self.tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž", "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz") end |
#p!(replace = " ") ⇒ Object
21 22 23 |
# File 'lib/rubyhelper/stringhelper.rb', line 21 def p!(replace= " ") return self.replace(self.p(replace)) end |
#scapitalize ⇒ Object
Capitalize a sequence
131 132 133 |
# File 'lib/rubyhelper/stringhelper.rb', line 131 def scapitalize return self.split.map(&:capitalize).join(' ') end |
#scapitalize! ⇒ Object
134 135 136 |
# File 'lib/rubyhelper/stringhelper.rb', line 134 def scapitalize! return self.replace(self.scapitalize) end |
#sha2 ⇒ Object
SHA2
80 81 82 |
# File 'lib/rubyhelper/stringhelper.rb', line 80 def sha2 Digest::SHA2.hexdigest(self) end |
#sha2! ⇒ Object
83 84 85 |
# File 'lib/rubyhelper/stringhelper.rb', line 83 def sha2! return self.replace(self.sha2) end |
#static(n, char = ' ') ⇒ Object
Param
n: number of char
char: char to replace if the initial str is too short
Get a str with a static length
91 92 93 94 95 96 97 |
# File 'lib/rubyhelper/stringhelper.rb', line 91 def static(n, char=' ') if self.size < n return self + char * (n - self.size).to_i else return self[0...n] end end |
#static!(n, char = ' ') ⇒ Object
98 99 100 |
# File 'lib/rubyhelper/stringhelper.rb', line 98 def static!(n, char=' ') return self.replace(self.static(n, char)) end |
#to_ascii(replace = "", case_mod = nil) ⇒ Object
Params
replace: a caracter to replace non-ascii chars
case_mod: nil (not changement), :upcase, :capitalize or :downcase
return a simple ascii string. Invalid characters will be replaced by “replace” (argument)
48 49 50 51 52 53 54 |
# File 'lib/rubyhelper/stringhelper.rb', line 48 def to_ascii(replace="", case_mod = nil) s = String.new self.p.each_char do |c| s += ((c.ord > 255) ? (replace.to_s) : (c)) end return s.to_case(case_mod) end |
#to_case(case_mod = :downcase) ⇒ Object
Params
case_mod: nil (not changement), :upcase, :capitalize or :downcase
permit to do upcase/downcase/capitalize easier with a simple param
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rubyhelper/stringhelper.rb', line 28 def to_case(case_mod = :downcase) case case_mod when :upcase return self.upcase when :downcase return self.downcase when :capitalize return self.capitalize else return self end end |
#to_case!(case_mod = :downcase) ⇒ Object
40 41 42 |
# File 'lib/rubyhelper/stringhelper.rb', line 40 def to_case!(case_mod = :downcase) return self.replace(self.to_case(case_mod)) end |
#to_f ⇒ Object
improvement of to_f to count “,” caracter as “.”
57 58 59 60 61 62 63 |
# File 'lib/rubyhelper/stringhelper.rb', line 57 def to_f s = self.dup self.gsub!(',', '.') f = super self.replace(s) return f end |
#to_i_wd(char = ' ') ⇒ Object
To_i with delimiter
66 67 68 |
# File 'lib/rubyhelper/stringhelper.rb', line 66 def to_i_wd(char=' ') self.delete(char.to_s).to_i end |
#to_plain(case_mod = nil, replace = " ") ⇒ Object
Params
case_mod: nil (not changement), :upcase, :capitalize or :downcase
replace: if a char is not utf8 valid, character will replace it
UTF-8 encoding and replace invalid chars, Remove accents from the string. Change the case as first argument
12 13 14 |
# File 'lib/rubyhelper/stringhelper.rb', line 12 def to_plain(case_mod = nil, replace= " ") return self.p.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').to_case(case_mod) end |
#to_t ⇒ Object
Returns true or false if the string if “true” or “false”
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rubyhelper/stringhelper.rb', line 103 def to_t case self when "true" return true when "false" return false else return nil end end |