Module: StringHelper

Included in:
String
Defined in:
lib/rubyhelper/stringhelper.rb

Instance Method Summary collapse

Instance Method Details

#^(k) ⇒ Object

CRYXOR (one time pad dirt application)



73
74
75
76
77
78
79
# File 'lib/rubyhelper/stringhelper.rb', line 73

def ^(k)
  str = ""
  self.size.times do |i|
    str << (self[i].ord ^ k[i % k.size].ord).chr
  end
  return str
end

#get_float(sign = true) ⇒ Object

as get_int but with . and ,

Params

sign: (true/false) if true, keep the - and + signs


149
150
151
152
# File 'lib/rubyhelper/stringhelper.rb', line 149

def get_float(sign = true)
  return self.gsub(/[^\-\+\d\.\,]/, "") if sign == true
  return self.gsub(/[^\d\.\,]/, "") if sign == true
end

#get_float!(sign = true) ⇒ Object



153
154
155
# File 'lib/rubyhelper/stringhelper.rb', line 153

def get_float!(sign = true)
  return self.replace(self.get_float(sign))
end

#get_int(sign = true) ⇒ Object

get only the digits and symbols in the string

Params

sign: (true/false) if true, keep the - and + signs


138
139
140
141
# File 'lib/rubyhelper/stringhelper.rb', line 138

def get_int(sign = true)
  return self.gsub(/[^\-\+\d]/, "") if sign == true
  return self.gsub(/[^\d]/, "")
end

#get_int!(sign = true) ⇒ Object



142
143
144
# File 'lib/rubyhelper/stringhelper.rb', line 142

def get_int!(sign = true)
  return self.replace(self.get_int(sign))
end

#pObject

Remove accents from the string, and replace it by the same letter in ASCII



17
18
19
20
# File 'lib/rubyhelper/stringhelper.rb', line 17

def p
  return self.tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
                 "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
end

#p!Object



21
22
23
# File 'lib/rubyhelper/stringhelper.rb', line 21

def p!
  return self.replace(self.p)
end

#scapitalizeObject

Capitalize a sequence



158
159
160
# File 'lib/rubyhelper/stringhelper.rb', line 158

def scapitalize
  return self.split.map(&:capitalize).join(' ')
end

#scapitalize!Object



161
162
163
# File 'lib/rubyhelper/stringhelper.rb', line 161

def scapitalize!
  return self.replace(self.scapitalize)
end

#sha2Object

SHA2 shortcuts



82
83
84
# File 'lib/rubyhelper/stringhelper.rb', line 82

def sha2
  Digest::SHA2.hexdigest(self)
end

#sha2!Object



85
86
87
# File 'lib/rubyhelper/stringhelper.rb', line 85

def sha2!
  return self.replace(self.sha2)
end

#static(n, char = ' ', place = :back) ⇒ Object

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

Param

n: number of char
char: char to replace if the initial str is too short
place: :begin/:front :end/:back :center/:middle


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubyhelper/stringhelper.rb', line 96

def static(n, char=' ', place= :back)
  char = char.to_s
  n = n.to_i
  if size < n
    case place
    when :begin, :front
      return char * (n - size).to_i + self
    when :center, :middle
      return char * ((n - size) / 2) + self + char * ((n - size) / 2 + (n - size) % 2)
    else
      return self + char * (n - size).to_i
    end
  else
    case place
    when :begin, :front
      return self[0...n]
    when :center, :middle
      return self[((-(size() +n - 1)) / 2)..((-(size() -n + 1)) / 2)]
    else
      return self[(-n)..(-1)]
    end
  end
end

#static!(n, char = ' ') ⇒ Object



119
120
121
# File 'lib/rubyhelper/stringhelper.rb', line 119

def static!(n, char=' ')
  return self.replace(self.static(n, char))
end

#to_ascii(replace = "", case_mod = nil) ⇒ Object

Return a simple ascii string. Invalid characters will be replaced by “replace” (argument) Accents are removed first and replaced by the equivalent ASCII letter

Params

replace: a caracter to replace non-ascii chars
case_mod: nil (not changement), :upcase, :capitalize or :downcase


49
50
51
52
53
54
55
# File 'lib/rubyhelper/stringhelper.rb', line 49

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_fObject

improvement of to_f to count “,” caracter as “.”



58
59
60
61
62
63
64
# File 'lib/rubyhelper/stringhelper.rb', line 58

def to_f
  s = self.dup
  self.gsub!(',', '.')
  f = super
  self.replace(s)
  return f
end

#to_ii(char = ' ') ⇒ Object

to_i with delimiter Example : “12.000.000”.to_ii => 12000000



68
69
70
# File 'lib/rubyhelper/stringhelper.rb', line 68

def to_ii(char=' ')
  self.delete(char.to_s).to_i
end

#to_plain(case_mod = nil, replace = " ") ⇒ Object

UTF-8 encoding and replace invalid chars, Remove accents from the string. Change the case as first argument if not nil

Params

case_mod: nil (not changement), :upcase, :capitalize or :downcase
replace: if a char is not utf8 valid, character will replace it


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_tObject

Returns true or false if the string if “true” or “false”. else nil



124
125
126
127
128
129
130
131
132
133
# File 'lib/rubyhelper/stringhelper.rb', line 124

def to_t
  case self
  when "true"
    return true
  when "false"
    return false
  else
    return nil
  end
end