Class: String
Overview
Extension class
Instance Method Summary collapse
-
#align_left ⇒ Object
Align a multi-line string to the left by removing as much spaces from the left as possible.
-
#blank? ⇒ Boolean
Check if string is empty.
-
#decode_visual ⇒ Object
Convert all not-printable characters encoded in hex format back to original.
- #dot_net_clean ⇒ Object
-
#encode_visual(regex = nil) ⇒ Object
Escape all not-printabe characters in hex format.
-
#escape_for_cmd ⇒ Object
Escape double quotes for usage in passing through scripts.
-
#escape_for_regexp ⇒ Object
Escape string for use in Regular Expressions.
-
#escape_for_sql ⇒ Object
Escape single quotes for usage in SQL statements.
-
#escape_for_string ⇒ Object
Escape double quotes for usage in code strings.
-
#quote ⇒ Object
Quote string for command-line use.
-
#remove_whitespace ⇒ Object
Convert whitespace into underscores.
-
#sort_form ⇒ Object
Create sortable object from string.
Instance Method Details
#align_left ⇒ Object
Align a multi-line string to the left by removing as much spaces from the left as possible.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/libis/tools/extend/string.rb', line 73 def align_left string = dup relevant_lines = string.split(/\r\n|\r|\n/).select { |line| line.size > 0 } indentation_levels = relevant_lines.map do |line| match = line.match(/^( +)[^ ]+/) match ? match[1].size : 0 end indentation_level = indentation_levels.min string.gsub! /^#{' ' * indentation_level}/, '' if indentation_level > 0 string end |
#blank? ⇒ Boolean
Check if string is empty
7 8 9 |
# File 'lib/libis/tools/extend/string.rb', line 7 def blank? self == '' end |
#decode_visual ⇒ Object
Convert all not-printable characters encoded in hex format back to original
68 69 70 |
# File 'lib/libis/tools/extend/string.rb', line 68 def decode_visual self.gsub(/_x([0-9a-f]{4})_/i) { [$1.to_i(16)].pack('U') } end |
#dot_net_clean ⇒ Object
52 53 54 |
# File 'lib/libis/tools/extend/string.rb', line 52 def dot_net_clean self.gsub /^(\d+|error|float|string);\\?#/, '' end |
#encode_visual(regex = nil) ⇒ Object
Escape all not-printabe characters in hex format
62 63 64 65 |
# File 'lib/libis/tools/extend/string.rb', line 62 def encode_visual(regex = nil) regex ||= /\W/ self.gsub(regex) { |c| '_x' + '%04x' % c.unpack('U')[0] + '_'} end |
#escape_for_cmd ⇒ Object
Escape double quotes for usage in passing through scripts
43 44 45 |
# File 'lib/libis/tools/extend/string.rb', line 43 def escape_for_cmd self.gsub(/"/) { |s| '\\\\\\' + s[0].to_s } end |
#escape_for_regexp ⇒ Object
Escape string for use in Regular Expressions
33 34 35 |
# File 'lib/libis/tools/extend/string.rb', line 33 def escape_for_regexp self.gsub(/[\.\+\*\(\)\{\}\|\/\\\^\$"']/) { |s| '\\' + s[0].to_s } end |
#escape_for_sql ⇒ Object
Escape single quotes for usage in SQL statements
48 49 50 |
# File 'lib/libis/tools/extend/string.rb', line 48 def escape_for_sql self.gsub(/'/) { |s| ($` == '' || $' == '' ? '' : '\'') + s[0].to_s } end |
#escape_for_string ⇒ Object
Escape double quotes for usage in code strings.
38 39 40 |
# File 'lib/libis/tools/extend/string.rb', line 38 def escape_for_string self.gsub(/"/) { |s| '\\' + s[0].to_s } end |
#quote ⇒ Object
Quote string for command-line use.
28 29 30 |
# File 'lib/libis/tools/extend/string.rb', line 28 def quote '\"' + self.gsub(/"/) { |s| '\\' + s[0] } + '\"' end |
#remove_whitespace ⇒ Object
Convert whitespace into underscores
57 58 59 |
# File 'lib/libis/tools/extend/string.rb', line 57 def remove_whitespace self.gsub(/\s/, '_') end |
#sort_form ⇒ Object
Create sortable object from string. Supports better natural sorting.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/libis/tools/extend/string.rb', line 12 def sort_form result = [] matcher = /^(\D*)(\d*)(.*)$/ self.split('.').each { |s| while !s.empty? and (x = matcher.match s) a = x[1].to_s.strip b = a.gsub(/[ _]/, '') result << [b.downcase, b, a] result << x[2].to_i s = x[3] end } result end |