Sha256: 88d32778dea00827860a7ecb9648824a10ee461f1086d65b5e4b31dc8deb1172
Contents?: true
Size: 1.65 KB
Versions: 2
Compression:
Stored size: 1.65 KB
Contents
# -*- encoding : utf-8 -*- class String #LUCENE_ESCAPE_REGEX = /(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\)/ LUCENE_ESCAPE_REGEX = /(\+|-|&&|\|\||!|\(|\)|{|}|\[|\]|`|"|~|\?|:|\\|\s)/ KEYBOARDS = { :en => %{qwertyuiop[]asdfghjkl;'zxcvbnm,./}, :ru => %{йцукенгшщзхъфывапролджэячсмитьбю/} } def lucene_escape self.gsub(LUCENE_ESCAPE_REGEX, "\\\\\\1") end def capitalize_first self.mb_chars[0].capitalize + self.mb_chars[1..-1] end def is_int? self =~ /^[-+]?[0-9]*$/ end def is_number? true if Float(self) rescue false end def to_utc begin Time.zone.parse(self).utc rescue Time.now.utc end end def no_html str = self.dup str.gsub!(/<\/?[^>]*>/, '') str.strip! str.gsub!(' ', ' ') str end def tr_lang(from=nil, to=nil) return '' if self.blank? unless from || to if KEYBOARDS[:en].index(self[0]) from, to = :en, :ru elsif KEYBOARDS[:ru].index(self[0]) from, to = :ru, :en else from, to = :en, :ru end end self.tr(KEYBOARDS[from], KEYBOARDS[to]) end def count_words clean_text.scan(/(\p{Alnum}+([-'.]\p{Alnum}+)*)/u).size end def words_count frequencies = Hash.new(0) downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 } frequencies end def self.randomize(length = 8) Array.new(length) { (rand(122-97) + 97).chr }.join end def clean_text coder = HTMLEntities.new coder.decode(self.no_html) end end unless ''.respond_to?(:each) String.class_eval do def each &block self.lines &block end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ab_admin-0.2.3 | lib/ab_admin/core_ext/string.rb |
ab_admin-0.2.2 | lib/ab_admin/core_ext/string.rb |