Class: String

Inherits:
Object show all
Defined in:
lib/libis/tools/extend/string.rb

Instance Method Summary collapse

Instance Method Details

#align_leftObject



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

Returns:

  • (Boolean)


3
4
5
# File 'lib/libis/tools/extend/string.rb', line 3

def blank?
  self == ''
end

#decode_visualObject



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_cleanObject



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_cmdObject



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_regexpObject



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_sqlObject



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_stringObject



38
39
40
# File 'lib/libis/tools/extend/string.rb', line 38

def escape_for_string
  self.gsub(/"/) { |s| '\\' + s[0].to_s }
end

#quoteObject



30
31
32
# File 'lib/libis/tools/extend/string.rb', line 30

def quote
  '\"' + self.gsub(/"/) { |s| '\\' + s[0] } + '\"'
end

#remove_whitespaceObject



54
55
56
# File 'lib/libis/tools/extend/string.rb', line 54

def remove_whitespace
  self.gsub(/\s/, '_')
end

#sort_formObject



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

#underscoreObject



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