Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/dic.rb,
lib/dic.rb,
lib/dic.rb,
lib/utf.rb,
lib/ipwry.rb,
lib/color.rb,
lib/do_as_rb19.rb,
lib/do_as_rb19.rb

Overview

为字符串类添加force_encoding和ord方法

Instance Method Summary collapse

Instance Method Details

#addTimChObject


49
50
51
# File 'lib/dic.rb', line 49

def addTimCh
  self << Time.now.hm.to_s
end

#alice_sayObject


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/dic.rb', line 201

def alice_say
  return if self.empty?
  url = 'http://www.pandorabots.com/pandora/talk?botid=f5d922d97e345aa1&skin=custom_input'
  p 'alice say'
  #$uri = uri=URI.parse(url)
  #$uri.open(
    #'Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*',
    #'Accept'=>'text/html',
    #'Referer'=> URI.escape(url),
    #'Accept-Language'=>'zh-cn',
    #'User-Agent'=> UserAgent
  #)
  agent = Mechanize.new
  agent.user_agent_alias = 'Linux Mozilla'
  agent.max_history = 0
  agent.open_timeout = 10
agent.read_timeout = 10
  #agent.cookies
  page = agent.get(url)
  #form          = page.form_with(:name => 'f')
  #form.input = 'how old are you ?'
  #page          = agent.submit(form)
  page = agent.post(url,{"input"=> self } )
  #File.new('a.txt','wb').puts page.body
  page.body.match(/<em>.+:(.+)<input type/m)[1].gsub(/alice/i,' kk ')
    .gsub!(/<.*?>/i,'') rescue '休息一下..'
end

#areaObject


33
34
35
36
37
38
39
# File 'lib/ipwry.rb', line 33

def area
  @@last_area.clear if @@last_area.size > @@last_area_size
  unless @@last_area.has_key? self
    @@last_area[self]= IpLocationSeeker.new.seek self
  end
  @@last_area[self]
end

#bObject


7
8
9
# File 'lib/do_as_rb19.rb', line 7

def b
  self
end

#blueObject


39
40
41
# File 'lib/color.rb', line 39

def blue
  "\e[34m#{self}#$normal"
end

#bluebObject


56
57
58
# File 'lib/color.rb', line 56

def blueb
  "\e[44m#{self}#$normal"
end

#c_rand(n = rand(99)) ⇒ Object


48
49
50
51
52
53
54
# File 'lib/color.rb', line 48

def c_rand(n=rand(99))
  #13 黄底
  #20 黄字
  #103 黄底白字
  c=Colors[n%Colors.size]
  "\e[#{c}m #{self}#$normal"
end

#clear_colorObject


30
31
32
# File 'lib/color.rb', line 30

def clear_color
  ANSI.uncolor self
end

#code_a2b(a, b) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'lib/utf.rb', line 16

def code_a2b(a,b)
  return self if a =~ /#{b}/i
   if RUBY_VERSION > '1.9' and defined? Encoding::Converter
     tmp = Encoding::Converter.new(a,b, :universal_newline => true)
     tmp.convert self rescue self
   else
     Iconv.conv("#{b}//IGNORE","#{a}//IGNORE",self)
   end
end

#cyanObject


45
46
47
# File 'lib/color.rb', line 45

def cyan
  "\e[36m#{self}#$normal"
end

#decode64Object Also known as: unbase64, ub64


33
34
35
# File 'lib/dic.rb', line 33

def decode64
  Base64.decode64 self
end

#en2zhObject


229
230
231
232
233
234
# File 'lib/dic.rb', line 229

def en2zh
	#return self if self.force_encoding("ASCII-8BIT") =~ CN_re #有中文
	return self unless self.ascii_only?
	flg = 'auto%7czh-CN'
	g_tr(self,flg)
end

#encode64Object Also known as: base64


38
39
40
# File 'lib/dic.rb', line 38

def encode64
  Base64.encode64 self
end

#force_encoding(s) ⇒ Object


14
15
16
# File 'lib/do_as_rb19.rb', line 14

def force_encoding(s)
  self
end

#gb_to_utf8Object


55
56
57
58
# File 'lib/utf.rb', line 55

def gb_to_utf8
   return $ec1.convert self if RUBY_VERSION > '1.9.2'
   Iconv.conv("UTF-8#{Ig}","GB18030#{Ig}",self)
end

#gbtoX(code) ⇒ Object


25
26
27
28
29
# File 'lib/utf.rb', line 25

def gbtoX(code)
  code_a2b('GB18030',code)
  #code_a2b('CP20936',code)
  #code_a2b('GB2312',code)
end

#getaddr_fromipObject


849
850
851
# File 'lib/dic.rb', line 849

def getaddr_fromip
  hostA(self,true)
end

#greenObject


33
34
35
# File 'lib/color.rb', line 33

def green
  "\e[32m#{self}#$normal"
end

#hostObject

域名转化为IP


41
42
43
44
45
46
47
48
# File 'lib/ipwry.rb', line 41

def host
  domain = self
  return 'IPV6' if domain =~ /^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i
  return self if domain =~ /(\d{1,3}\.){3}\d{1,3}/
  domain = domain.gsub(/\/.*/i,'')
  return domain unless domain.include?('.')
  return Resolv.getaddress(domain) rescue domain
end

#iblueObject


67
68
69
# File 'lib/color.rb', line 67

def iblue
  "\x033#{self}\x030"
end

#icolor(n = rand(99)) ⇒ Object

irc color


64
65
66
# File 'lib/color.rb', line 64

def icolor(n=rand(99))
  self
end

#ii(s = ['☘',"\322\211"][rand(2)]) ⇒ Object

“343213206” ㏠


46
47
48
# File 'lib/dic.rb', line 46

def ii(s=['',"\322\211"][rand(2)])
  self.split(//u).join(s)
end

#md5Object


52
53
54
# File 'lib/dic.rb', line 52

def md5
  Digest::MD5.digest self
end

#md5hexObject


55
56
57
# File 'lib/dic.rb', line 55

def md5hex
  Digest::MD5.hexdigest self
end

#ordObject


17
18
19
# File 'lib/do_as_rb19.rb', line 17

def ord
  self[0]
end

#pinkObject


42
43
44
# File 'lib/color.rb', line 42

def pink
  "\e[35m#{self}#$normal"
end

#prepend(s) ⇒ Object


10
11
12
# File 'lib/do_as_rb19.rb', line 10

def prepend s
  self.replace s+self
end

#redbObject


59
60
61
# File 'lib/color.rb', line 59

def redb
  "\e[41m#{self}#$normal"
end

#rot13Object


42
43
44
# File 'lib/dic.rb', line 42

def rot13
	self.tr "A-Za-z", "N-ZA-Mn-za-m"
end

#slice_u!(n) ⇒ Object


21
22
23
24
25
# File 'lib/dic.rb', line 21

def slice_u!(n)
  self.force_encoding('ascii-8bit')
  self.slice!(n) #Deletes the specified portion from str
  self.force_encoding('utf-8')
end

#to_hex(s = ' ') ⇒ Object


65
66
67
# File 'lib/utf.rb', line 65

def to_hex(s=' ')
   self.each_byte.map{|b| "%02X" % b}.join(s)
end

#to_utf8Object Also known as: toutf8


59
60
61
62
# File 'lib/utf.rb', line 59

def to_utf8
   return $ec1.convert self if RUBY_VERSION > '1.9.2'
   Iconv.conv("UTF-8#{Ig}","GB18030#{Ig}",self)
end

#togbObject Also known as: to_gb


42
43
44
45
46
47
48
# File 'lib/utf.rb', line 42

def togb
   if RUBY_VERSION > '1.9.2'
      $ec2.convert self rescue self
   else
      Iconv.conv("GB2312#{Ig}","UTF-8#{Ig}",self)
   end
end

#togb2312Object


31
32
33
34
# File 'lib/utf.rb', line 31

def togb2312
   return $ec2.convert self if RUBY_VERSION > '1.9.2'
   Iconv.conv("CP20936#{Ig}","UTF-8#{Ig}",self)
end

#togbkObject


35
36
37
38
39
40
41
# File 'lib/utf.rb', line 35

def togbk
   if RUBY_VERSION > '1.9.2'
      $ec2.convert self rescue self
   else
      Iconv.conv("GBK#{Ig}","UTF-8#{Ig}",self)
   end
end

#unescapeHTMLObject

整理html里的 &nbsp; 等转义串


60
61
62
63
# File 'lib/dic.rb', line 60

def unescapeHTML
  #CGI.unescapeHTML(self) rescue self
  HTMLEntities.new.decode(self) rescue self
end

#uri_decodeObject


27
28
29
# File 'lib/dic.rb', line 27

def uri_decode
    URI.decode self
end

#uri_encodeObject


30
31
32
# File 'lib/dic.rb', line 30

def uri_encode
   URI.encode self
end

#utf8_to_gbObject


51
52
53
54
# File 'lib/utf.rb', line 51

def utf8_to_gb
   return $ec2.convert self if RUBY_VERSION > '1.9.2'
   Iconv.conv("GB18030#{Ig}","UTF-8#{Ig}",self)
end

#yellowObject


36
37
38
# File 'lib/color.rb', line 36

def yellow
  "\e[33m#{self}#$normal"
end

#zh2enObject


235
236
237
238
239
240
# File 'lib/dic.rb', line 235

def zh2en
	#return self if self.force_encoding("ASCII-8BIT") !~ CN_re #无中文
	return self if self.ascii_only?
	flg = 'zh-CN%7cen'
	g_tr(self,flg)
end