Class: String

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

Overview

Extension class

Instance Method Summary collapse

Instance Method Details

#align_leftObject

Align a multi-line string to the left by removing as much spaces from the left as possible.



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/libis/tools/extend/string.rb', line 107

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/libis/tools/extend/string.rb', line 41

def blank?
  self == ''
end

#camelize(first_letter = :upper) ⇒ Object

from activesupport



5
6
7
8
9
10
11
# File 'lib/libis/tools/extend/string.rb', line 5

def camelize(first_letter = :upper)
  if first_letter == :upper
    gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self[0..0].downcase + camelize[1..-1]
  end
end

#constantizeObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/libis/tools/extend/string.rb', line 13

def constantize
  names = split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#dasherizeObject



24
25
26
# File 'lib/libis/tools/extend/string.rb', line 24

def dasherize
  gsub(/_/, '-')
end

#decode_visualObject

Convert all not-printable characters encoded in hex format back to original



102
103
104
# File 'lib/libis/tools/extend/string.rb', line 102

def decode_visual
  self.gsub(/_x([0-9a-f]{4})_/i) { [$1.to_i(16)].pack('U') }
end

#demodulizeObject



28
29
30
# File 'lib/libis/tools/extend/string.rb', line 28

def demodulize
  gsub(/^.*::/, '')
end

#dot_net_cleanObject



86
87
88
# File 'lib/libis/tools/extend/string.rb', line 86

def dot_net_clean
  self.gsub /^(\d+|error|float|string);\\?#/, ''
end

#encode_visual(regex = nil) ⇒ Object

Escape all not-printabe characters in hex format



96
97
98
99
# File 'lib/libis/tools/extend/string.rb', line 96

def encode_visual(regex = nil)
  regex ||= /\W/
  self.gsub(regex) { |c| '_x' + '%04x' % c.unpack('U')[0] + '_'}
end

#escape_for_cmdObject

Escape double quotes for usage in passing through scripts



77
78
79
# File 'lib/libis/tools/extend/string.rb', line 77

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

#escape_for_regexpObject

Escape string for use in Regular Expressions



67
68
69
# File 'lib/libis/tools/extend/string.rb', line 67

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

#escape_for_sqlObject

Escape single quotes for usage in SQL statements



82
83
84
# File 'lib/libis/tools/extend/string.rb', line 82

def escape_for_sql
  self.gsub(/'/) { |s| ($` == '' || $' == '' ? '' : '\'') + s[0].to_s }
end

#escape_for_stringObject

Escape double quotes for usage in code strings.



72
73
74
# File 'lib/libis/tools/extend/string.rb', line 72

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

#quoteObject

Quote string for command-line use.



62
63
64
# File 'lib/libis/tools/extend/string.rb', line 62

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

#remove_whitespaceObject

Convert whitespace into underscores



91
92
93
# File 'lib/libis/tools/extend/string.rb', line 91

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

#sort_formObject

Create sortable object from string. Supports better natural sorting.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/libis/tools/extend/string.rb', line 46

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



32
33
34
35
36
37
38
# File 'lib/libis/tools/extend/string.rb', line 32

def underscore
  gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end