class String

Extension class

Constants

BLANK_REGEX

Public Instance Methods

align_left() click to toggle source

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

# File lib/libis/tools/extend/string.rb, line 103
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?() click to toggle source
# File lib/libis/tools/extend/empty.rb, line 23
def blank?
  empty? || BLANK_REGEX.match?(self)
end
camelize(first_letter = :upper) click to toggle source

from activesupport

# 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
constantize() click to toggle source
# 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
dasherize() click to toggle source
# File lib/libis/tools/extend/string.rb, line 24
def dasherize
  gsub(/_/, '-')
end
decode_visual() click to toggle source

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

# File lib/libis/tools/extend/string.rb, line 98
def decode_visual
  self.gsub(/_x([0-9a-f]{4})_/i) { [$1.to_i(16)].pack('U') }
end
demodulize() click to toggle source
# File lib/libis/tools/extend/string.rb, line 28
def demodulize
  gsub(/^.*::/, '')
end
dot_net_clean() click to toggle source
# File lib/libis/tools/extend/string.rb, line 82
def dot_net_clean
  self.gsub /^(\d+|error|float|string);\\?#/, ''
end
encode_visual(regex = nil) click to toggle source

Escape all not-printabe characters in hex format

# File lib/libis/tools/extend/string.rb, line 92
def encode_visual(regex = nil)
  regex ||= /\W/
  self.gsub(regex) { |c| '_x' + '%04x' % c.unpack('U')[0] + '_'}
end
escape_for_cmd() click to toggle source

Escape double quotes for usage in passing through scripts

# File lib/libis/tools/extend/string.rb, line 73
def escape_for_cmd
  self.gsub(/"/) { |s| '\\\\\\' + s[0].to_s }
end
escape_for_regexp() click to toggle source

Escape string for use in Regular Expressions

# File lib/libis/tools/extend/string.rb, line 63
def escape_for_regexp
  self.gsub(/[\.\+\*\(\)\{\}\|\/\\\^\$"']/) { |s| '\\' + s[0].to_s }
end
escape_for_sql() click to toggle source

Escape single quotes for usage in SQL statements

# File lib/libis/tools/extend/string.rb, line 78
def escape_for_sql
  self.gsub(/'/) { |s| ($` == '' || $' == '' ? '' : '\'') + s[0].to_s }
end
escape_for_string() click to toggle source

Escape double quotes for usage in code strings.

# File lib/libis/tools/extend/string.rb, line 68
def escape_for_string
  self.gsub(/"/) { |s| '\\' + s[0].to_s }
end
quote() click to toggle source

Quote string for command-line use.

# File lib/libis/tools/extend/string.rb, line 58
def quote
  '\"' + self.gsub(/"/) { |s| '\\' + s[0] } + '\"'
end
remove_whitespace() click to toggle source

Convert whitespace into underscores

# File lib/libis/tools/extend/string.rb, line 87
def remove_whitespace
  self.gsub(/\s/, '_')
end
sort_form() click to toggle source

Create sortable object from string. Supports better natural sorting.

# File lib/libis/tools/extend/string.rb, line 42
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() click to toggle source
# 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