Class: String
Instance Method Summary collapse
- #align_left ⇒ Object
- #blank? ⇒ Boolean
- #decode_visual ⇒ Object
- #dot_net_clean ⇒ Object
- #encode_visual(regex = nil) ⇒ Object
- #escape_for_cmd ⇒ Object
- #escape_for_regexp ⇒ Object
- #escape_for_sql ⇒ Object
- #escape_for_string ⇒ Object
- #quote ⇒ Object
- #remove_whitespace ⇒ Object
- #sort_form ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#align_left ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/libis/tools/extend/string.rb', line 67 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
3 4 5 |
# File 'lib/libis/tools/extend/string.rb', line 3 def blank? self == '' end |
#decode_visual ⇒ Object
63 64 65 |
# File 'lib/libis/tools/extend/string.rb', line 63 def decode_visual self.gsub(/_x([0-9a-f]{4})_/i) { [$1.to_i(16)].pack('U') } end |
#dot_net_clean ⇒ Object
50 51 52 |
# File 'lib/libis/tools/extend/string.rb', line 50 def dot_net_clean self.gsub /^(\d+|error|float|string);\\?#/, '' end |
#encode_visual(regex = nil) ⇒ Object
58 59 60 61 |
# File 'lib/libis/tools/extend/string.rb', line 58 def encode_visual(regex = nil) regex ||= /\W/ self.gsub(regex) { |c| '_x' + '%04x' % c.unpack('U')[0] + '_'} end |
#escape_for_cmd ⇒ Object
42 43 44 |
# File 'lib/libis/tools/extend/string.rb', line 42 def escape_for_cmd self.gsub(/"/) { |s| '\\\\\\' + s[0].to_s } end |
#escape_for_regexp ⇒ Object
34 35 36 |
# File 'lib/libis/tools/extend/string.rb', line 34 def escape_for_regexp self.gsub(/[\.\+\*\(\)\{\}\|\/\\\^\$"']/) { |s| '\\' + s[0].to_s } end |
#escape_for_sql ⇒ Object
46 47 48 |
# File 'lib/libis/tools/extend/string.rb', line 46 def escape_for_sql self.gsub(/'/) { |s| ($` == '' || $' == '' ? '' : '\'') + s[0].to_s } end |
#escape_for_string ⇒ Object
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
30 31 32 |
# File 'lib/libis/tools/extend/string.rb', line 30 def quote '\"' + self.gsub(/"/) { |s| '\\' + s[0] } + '\"' end |
#remove_whitespace ⇒ Object
54 55 56 |
# File 'lib/libis/tools/extend/string.rb', line 54 def remove_whitespace self.gsub(/\s/, '_') end |
#sort_form ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/libis/tools/extend/string.rb', line 7 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 |
#underscore ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/libis/tools/extend/string.rb', line 22 def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). tr('-', '_'). downcase end |