class String

Constants

ANSI_BOLD
ANSI_CLEAR

Embed in a String to clear all previous ANSI sequences.

ANSI_UNDERLINE
BLACK

Colors

BLUE
CYAN
GREEN
MAGENTA
RED
WHITE
WIN_1251_ENCODING
YELLOW

Public Class Methods

natcmp(str1, str2) click to toggle source
# File lib/string_tools/core_ext/string.rb, line 44
def self.natcmp(str1, str2)
  str1, str2 = str1.dup, str2.dup
  compare_expression = /^(\D*)((?:\d+(?:\.\d+)?)*)(.*)$/

  # Remove all whitespace
  str1.gsub!(/\s*/, '')
  str2.gsub!(/\s*/, '')

  while (str1.length > 0) or (str2.length > 0)
    # Extract non-digits, digits and rest of string
    str1 =~ compare_expression
    chars1, num1, str1 = $1.dup, $2.dup, $3.dup

    str2 =~ compare_expression
    chars2, num2, str2 = $1.dup, $2.dup, $3.dup

    # Compare the non-digits
    case (chars1 <=> chars2)
    when 0 # Non-digits are the same, compare the digits...
      # If either number begins with a zero, then compare alphabetically,
      # otherwise compare numerically
      if !(num1[0] == 48 && num1[1] != 46) and !(num2[0] == 48 && num2[1] != 46)
        num1, num2 = num1.to_f, num2.to_f
      end

      case (num1 <=> num2)
      when -1 then return -1
      when 1 then return 1
      end
    when -1 then return -1
    when 1 then return 1
    end # case

  end # while

  # Strings are naturally equal
  0
end

Public Instance Methods

bold() click to toggle source

Synopsys

Make text bolder (for ASCII terminals)
# File lib/string_tools/core_ext/string.rb, line 136
def bold
  surround_with_ansi(ANSI_BOLD)
end
colorize(color, bold_or_options = nil) click to toggle source

Synopsys

Colorize string (for terminals)
Does not work with sprintf yet

Usage

"ln -s".colorize(:red)

Args

+color+ - symbol, one of the following (black, white, red, green, yellow, blue, magenta, cyan)
+bold_or_options+ - True/False or Hash
# File lib/string_tools/core_ext/string.rb, line 113
def colorize(color, bold_or_options = nil)
  is_bold      = bold_or_options.is_a?(TrueClass)
  is_underline = false

  if bold_or_options.is_a?(Hash)
    is_bold    ||= bold_or_options[:bold]
    is_underline = bold_or_options[:underline]
  end

  raise ArgumentError('Color must be a symbol') unless color.is_a?(Symbol)
  color_const = color.to_s.upcase.to_sym

  raise ArgumentError('Unknown color') unless self.class.const_defined?(color_const)
  ascii_color = self.class.const_get(color_const)

  s = surround_with_ansi(ascii_color)
  s = s.bold      if is_bold
  s = s.underline if is_underline
  s
end
detect_encoding() click to toggle source

shorthand

# File lib/string_tools/core_ext/string.rb, line 160
def detect_encoding
  e = ::CharDet.detect(self)["encoding"]
  e = WIN_1251_ENCODING if StringTools::CharDet::CP1251_COMPATIBLE_ENCODINGS.include?(e)
  e
end
mb_downcase() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 198
def mb_downcase
  # https://github.com/rails/rails/commit/393e19e508a08ede0f5037bccb984e3eb252d579
  if ActiveSupport::VERSION::STRING >= '4.0.0' && ActiveSupport::VERSION::STRING <= '4.2.0'
    ActiveSupport::Multibyte::Unicode.send(:database).codepoints
  end

  mb_chars.downcase.to_s
end
naturalized() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 40
def naturalized
  scan(/[^\d\.]+|[\d\.]+/).map{|f| f.match(/^\d+(\.\d+)?$/) ? f.to_f : f }
end
remove_colors() click to toggle source

Synopsys

remove colors from colorized string
# File lib/string_tools/core_ext/string.rb, line 148
def remove_colors
  gsub(/\e\[\d+m/, '')
end
remove_nonprintable() click to toggle source

Public: returns copy of string with all non-printable characters removed

Examples

"q\uFFFEw\uFFFFe\uFFF0r\uFDD0t\uFDEFy".remove_nonprintable
# => "qwerty"

Returns String

# File lib/string_tools/core_ext/string.rb, line 215
def remove_nonprintable
  gsub(/[^[:print:]\n\t]/i, '')
end
remove_nonprintable!() click to toggle source

Public: removes all non-printable characters from string

Examples

"q\uFFFEw\uFFFFe\uFFF0r\uFDD0t\uFDEFy".remove_nonprintable!
# => "qwerty"

Returns String

# File lib/string_tools/core_ext/string.rb, line 227
def remove_nonprintable!
  replace(remove_nonprintable)
end
strip_tags() click to toggle source

возвращает строку из которой удалены HTML-теги символы <>&“ остаются без изменения

# File lib/string_tools/core_ext/string.rb, line 21
def strip_tags
  Nokogiri::HTML5.parse(self).content
end
to_b() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 32
def to_b
  StringTools::String.new(self).to_b
end
to_cp1251() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 188
def to_cp1251
  encode 'cp1251', :undef => :replace, :invalid => :replace
rescue
  ''
end
to_cp1251!() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 194
def to_cp1251!
  self.replace(self.to_cp1251)
end
to_f_with_strip_comma() click to toggle source

'11,3'.to_f

> 11.3

# File lib/string_tools/core_ext/string.rb, line 27
def to_f_with_strip_comma
  self.gsub(/,/,'.').to_f_without_strip_comma
end
to_punycode() click to toggle source

Выполняет преобразование строки в punycode.

# File lib/string_tools/core_ext/string.rb, line 84
def to_punycode
  Addressable::URI.parse(self).normalize.to_s
end
to_script_safe_json() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 36
def to_script_safe_json
  self.to_json.gsub('</script>', '</" + "script>" + "')
end
to_utf8() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 170
def to_utf8
  # и так utf
  return self if is_utf8?

  enc = detect_encoding

  # если utf или английские буквы, то тоже ок
  return self if ['utf-8', 'ascii'].include?(enc)

  # если неизвестная каша, то возвращаем пустую строку
  return '' if enc.nil?

  # иначе пытаемся перекодировать
  encode 'utf-8', enc, :undef => :replace, :invalid => :replace
rescue
  ''
end
to_utf8!() click to toggle source
# File lib/string_tools/core_ext/string.rb, line 166
def to_utf8!
  self.replace(self.to_utf8)
end
underline() click to toggle source

Synopsys

Make text underlined (for ASCII terminals)
# File lib/string_tools/core_ext/string.rb, line 142
def underline
  surround_with_ansi(ANSI_UNDERLINE)
end

Private Instance Methods

protect_escape_of(ascii_seq) click to toggle source
# File lib/string_tools/core_ext/string.rb, line 237
def protect_escape_of(ascii_seq)
  gsub(Regexp.new(Regexp.escape(ANSI_CLEAR)), "#{ANSI_CLEAR}#{ascii_seq}")
end
surround_with_ansi(ascii_seq) click to toggle source
# File lib/string_tools/core_ext/string.rb, line 233
def surround_with_ansi(ascii_seq)
  "#{ascii_seq}#{protect_escape_of(ascii_seq)}#{ANSI_CLEAR}"
end