锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲成人免费网站,亚洲AV成人无码久久WWW,久久久久亚洲av无码专区导航http://m.tkk7.com/Blake/category/1587.htmlJava Toxication!zh-cnTue, 27 Feb 2007 18:45:42 GMTTue, 27 Feb 2007 18:45:42 GMT60Ruby Jewellery [Updating...]http://m.tkk7.com/Blake/articles/5475.htmlBlake HANBlake HANThu, 02 Jun 2005 08:37:00 GMThttp://m.tkk7.com/Blake/articles/5475.htmlhttp://m.tkk7.com/Blake/comments/5475.htmlhttp://m.tkk7.com/Blake/articles/5475.html#Feedback0http://m.tkk7.com/Blake/comments/commentRss/5475.htmlhttp://m.tkk7.com/Blake/services/trackbacks/5475.htmlThis article will include many usefull examples. Let's enjoy Ruby!


 HTTP Client
Here is am example shows how to write HTTP client with Ruby.
require 'net/http'
def fetch(loc, port, url)
   z
=
nil
   h
=Net::HTTP.new(loc
,port)
   begin
     resp, data 
= h.get
(url,z)
     puts 
"Code = #{resp.code} #{resp.message}"

     resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val}
     p data
   rescue Net::ProtoFatalError 
=>
 detail
     head 
=
 detail.data
     head.
each {|key,val| puts "#{key}    #{val}"
}
     
if head.code=="404"

         puts "File not Found"
     elsif head.code=="401"
         print "Authorization Required\n User name?"
         gets
         uname
=
$_.chomp
         
print "Password?"

         gets
         pwd
=
$_.chomp
         unpwd
=uname + ":" +
 pwd
         puts unpwd
         hval
=[unpwd].pack("m"
)
         z
={'Authorization' => 'Basic ' + hval}

         retry
     
end

   rescue SystemCallError
         puts 
"Can't connect"
   end
end

fetch(
'www.google.com', 80, '/')



Send email
Send email with Ruby:

 1 require 'net/smtp'
 2 SMTP_HOST = "192.168.60.99"
 3 

 4 def send(from, to, subject, msg)
 5   mail = "To: #{to}\r\n" +

 6          "From: #{from}\r\n" +
 7          "Subject: #{subject}\r\n" +
 8          "\r\n" +
 9          msg
10 

11   Net::SMTP.start(SMTP_HOST) do |smtp|
12     smtp.send_mail(mail, from, to
)
13   end

14 end
15 
16 from = "blake@192.168.60.99"
17 to = ["blake@192.168.60.99"]
18 send(from, to"test""Just a test!\ntest")

Notice:
1. 'mail' is the email body, it use "\r\n" to separate the mail header and mail context. So if you only want to send context without header, you should write: "mail = "\r\n" + msg". Otherwise your email will lost the context.


Calculate the total line number of files in one folder
This is one small tools to get the total file line number of on folder.
 
 1POST_FIX = %w{java jsp html xml properties}
 2EXCLUDE_DIR =
 %w{CVS}
 3$displayStr = ""

 4$fileReg = ""
 5$dirReg = "\\.|\\.\\.|"
 6

 7def sumLine(fileName)
 8  file =
 open fileName
 9
  begin
10    while
 file.gets()
11    end

12  ensure
13
    file.close
14  end

15  $.
16end

17
18def iteDir(name, level=0
)
19  lines = 0

20  dir = Dir.open name
21  rex = /\+ "java" +$|\.jsp$|\.html$/

22  begin
23    dir.each do
 |fileName|
24      fileName = dir.path + '/' + fileName

25      if File.ftype(fileName) =/directory/
26        if not fileName =~ Regexp.new($dirReg)
27          lines += iteDir(fileName, level+1
)
28        else

29          next
30        end
31      elsif fileName =~ Regexp.new($fileReg)
32        lines +=
 sumLine(fileName)
33      end

34    end
35  ensure
36
    dir.close
37  end

38  str = ""
39  for i in 0..(level-1)
40    str += "\t|"

41  end
42  if level != 0
43    str += "---"
44  end
45  $displayStr = "#{str}#{name}\t#{lines} lines\n#{$displayStr}"
46  lines
47end

48
49POST_FIX.each do
 |postfix|
50  $fileReg += "\.#{postfix}$|"

51end
52$fileReg = $fileReg.chop
53

54EXCLUDE_DIR.each do
 |excludeDir|
55  $dirReg += "^#{excludeDir}$|"

56end
57$dirReg = $dirReg.chop
58

59print "Total #{iteDir("E:/_MyWorkSpace/IDEA_Proj")} lines\n"

60print $displayStr

Keystone:
   1. Ruby I/O access. Dir and File
   2. Regexp & String -> Regexp
   3. Some kinds of variables
   4. Array operation



Technorati Tags: Ping Technorati



Blake HAN 2005-06-02 16:37 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 免费观看亚洲人成网站| 在线亚洲高清揄拍自拍一品区| 亚洲性色AV日韩在线观看| 亚洲三级在线免费观看| 亚洲精品无码国产片| 国内精自视频品线六区免费| 亚洲视频在线观看| 中文字幕视频免费| 亚洲精选在线观看| 久久成人无码国产免费播放| 亚洲妇女水蜜桃av网网站| 亚洲精品无码成人AAA片| 亚洲av中文无码乱人伦在线观看| 在线免费观看你懂的| 亚洲一级大黄大色毛片| 天堂亚洲国产中文在线| 在线免费不卡视频| 国产精品亚洲精品久久精品 | 亚洲精品无码成人片在线观看| 免费精品国产自产拍在线观看 | 好紧我太爽了视频免费国产| 亚洲美女又黄又爽在线观看| 精品一区二区三区免费| 亚洲色偷偷偷网站色偷一区| 男人的好看免费观看在线视频| 亚洲精品无码久久久久YW| 国产L精品国产亚洲区久久| 亚洲av无码一区二区三区观看| 成人免费视频观看无遮挡| 真正全免费视频a毛片| 亚洲男同帅GAY片在线观看| 一色屋成人免费精品网站| 精品亚洲成A人在线观看青青| 亚洲一区二区三区偷拍女厕 | 亚洲视屏在线观看| 日韩免费a级在线观看| 在线观看片免费人成视频无码| 亚洲av日韩av综合| 亚洲日本中文字幕一区二区三区| 99精品热线在线观看免费视频| 亚洲AV无码一区二区三区网址|