class String

Public Instance Methods

add_slash() click to toggle source
# File lib/useful_string_extensions/string.rb, line 44
def add_slash
  self == "" ? "" : (self.ends_with?("/") ? self : "#{self}/")
end
bstrip() click to toggle source
# File lib/useful_string_extensions/string.rb, line 36
def bstrip
  self.lstrip.rstrip
end
capitalize() click to toggle source
# File lib/useful_string_extensions/string.rb, line 109
def capitalize
  Unicode::capitalize(self)
end
change_plus() click to toggle source
# File lib/useful_string_extensions/string.rb, line 48
def change_plus
  self.gsub("+7", "8")
end
clearify() click to toggle source
# File lib/useful_string_extensions/string.rb, line 113
def clearify
  self.blank? ? "" : self.gsub('"', "'")
end
detect_encoding() click to toggle source
# File lib/useful_string_extensions/string.rb, line 20
def detect_encoding
  CharDet.detect(text)['encoding'] rescue "utf-8"
end
encode_from_1251_to_utf8() click to toggle source
# File lib/useful_string_extensions/string.rb, line 158
def encode_from_1251_to_utf8
  self.force_encoding("windows-1251").encode("utf-8", :invalid => :replace)
end
encode_to_utf8() click to toggle source
# File lib/useful_string_extensions/string.rb, line 162
def encode_to_utf8
  self.encode("utf-8", :invalid => :replace)
end
end_stars() click to toggle source
# File lib/useful_string_extensions/string.rb, line 101
def end_stars
  self.sanitize_to_sphinx.split(" ").collect {|w| "#{w}*" if w.size > 2}.join(" ")
end
generate_password(len = 7)
generate_secret_code(len = 7) click to toggle source
# File lib/useful_string_extensions/string.rb, line 87
def generate_secret_code(len = 7)
  chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('1'..'9').to_a - ['o', 'O', 'i', 'I']
  return Array.new(len) { chars[rand(chars.size)] }.join
end
Also aliased as: generate_password
get_extension() click to toggle source
# File lib/useful_string_extensions/string.rb, line 64
def get_extension
  File.extname(self)
end
get_id_from_url() click to toggle source
# File lib/useful_string_extensions/string.rb, line 28
def get_id_from_url
  self.split("-").last
end
in_one_line() click to toggle source
# File lib/useful_string_extensions/string.rb, line 24
def in_one_line
  self.gsub("\n",'').gsub("\r",'').gsub("\t",'')
end
is_valid_email?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 125
def is_valid_email?
  !(self =~ CustomEmailValidator.email_regex).nil?
end
is_valid_phone?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 129
def is_valid_phone?
  !(self =~ CustomPhoneValidator.world_phone_regex).nil?
end
is_valid_phone_from_russia?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 137
def is_valid_phone_from_russia?
  !(self =~ CustomPhoneValidator.russian_phone_regex).nil?
end
is_valid_phone_from_sng?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 133
def is_valid_phone_from_sng?
  !(self =~ CustomPhoneValidator.sng_phone_regex).nil?
end
is_valid_phone_from_ukrain?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 141
def is_valid_phone_from_ukrain?
  !(self =~ CustomPhoneValidator.ukrainian_phone_regex).nil?
end
normalize_phone() click to toggle source
# File lib/useful_string_extensions/string.rb, line 145
def normalize_phone
  self == "" ? "" : self.gsub(/\D+/,'')
end
path_to_a() click to toggle source
# File lib/useful_string_extensions/string.rb, line 105
def path_to_a
  self.gsub(",", '').split(' ').compact
end
remove_extension() click to toggle source
# File lib/useful_string_extensions/string.rb, line 60
def remove_extension
  File.basename(self, '.*')
end
remove_promo() click to toggle source
# File lib/useful_string_extensions/string.rb, line 68
def remove_promo
  self.include?("::") ? self.split("::")[1] : self
end
remove_stars() click to toggle source
# File lib/useful_string_extensions/string.rb, line 83
def remove_stars
  self.blank? ? "" : self.gsub('*', '')
end
replace_quotes() click to toggle source
# File lib/useful_string_extensions/string.rb, line 12
def replace_quotes
  self == "" ? "" : self.gsub(""", '"')
end
sanitize_to_sphinx() click to toggle source
# File lib/useful_string_extensions/string.rb, line 93
def sanitize_to_sphinx
  self == "" ? "" : Unicode.downcase(self).gsub(/[^a-zа-яёЁ0-9\*]/, ' ').split("[")[0].split(" ").compact.join(" ")
end
show?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 16
def show?
  self.blank? ? "Не указано" : self
end
split_stars() click to toggle source
# File lib/useful_string_extensions/string.rb, line 97
def split_stars
  self == "" ? "" : self.split(" ").collect {|w| "*#{w}*" if w.size > 2}.join(" ")
end
string_from_file() click to toggle source
# File lib/useful_string_extensions/string.rb, line 3
def string_from_file
  data = ''
  f = File.open(self, "r")
  f.each_line do |line|
    data += line
  end
  data
end
to_cached_page(current_page) click to toggle source
# File lib/useful_string_extensions/string.rb, line 170
def to_cached_page(current_page)
  self.gsub("?page=", "/page-").gsub("/page-#{current_page}", "").gsub("/page-1", "")
end
to_clean() click to toggle source
# File lib/useful_string_extensions/string.rb, line 149
def to_clean
  allowed_tags = {
    :elements => %w[p a ul ol li u],
    :attributes => {'a' => ['href', 'title']},
    :protocols => {'a' => {'href' => ['http', 'https', 'mailto']}}
  }
  self == "" ? "" : Sanitize.clean(self, allowed_tags)
end
to_keywords() click to toggle source
# File lib/useful_string_extensions/string.rb, line 52
def to_keywords
  self == '' ? '' : self.gsub(/\(\)\,\./, '').split(" ").collect {|e| Unicode.downcase(e)}.join(", ")
end
to_page_slug() click to toggle source
# File lib/useful_string_extensions/string.rb, line 117
def to_page_slug
  self.gsub(/^\//, '').gsub(".html", "")
end
to_print() click to toggle source
# File lib/useful_string_extensions/string.rb, line 32
def to_print
  self.gsub("\t", '').gsub("\n", '').gsub("\r", '')
end
to_production_zones() click to toggle source
# File lib/useful_string_extensions/string.rb, line 166
def to_production_zones
  self.gsub("local", "ru").gsub("en", "com").gsub(":3000", "")
end
to_simple_xhtml() click to toggle source
# File lib/useful_string_extensions/string.rb, line 56
def to_simple_xhtml
  self == '' ? '' : self.gsub("<br>", '<br />')
end
to_slug() click to toggle source
# File lib/useful_string_extensions/string.rb, line 72
def to_slug
  Russian.translit(self).downcase.gsub(/[^a-z0-9]+/, '-').strip.chomp('-')

  # другой вариант
  # initial = Russian::translit(self.name).gsub(/[^A-Za-z0-9\s\-]/, "")[0,40].strip.gsub(/\s+/, "-").downcase
end
to_widgets_array() click to toggle source
# File lib/useful_string_extensions/string.rb, line 40
def to_widgets_array
  self.split(",").collect{|e| e.gsub("widget_",'').to_i}
end
valide_of_email?() click to toggle source
# File lib/useful_string_extensions/string.rb, line 121
def valide_of_email?
  self =~ /(^$)|(^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\.[a-z]{2,}$)/
end