- 论坛徽章:
- 0
|
今天初試 ruby
買了本Enjoy Ruby Programing 中譯本兩個多星期,斷斷續續看到第八章,今天
放假,閉了書試著寫個ruby script, 看看懂了多少, 在cygwin 下沒有free
這個command, 從前我是寫個簡單的shell script 代替,只是以 /proc/meminfo
裡面的資訊作個簡單版,那就試著用 ruby 改寫.
寫的時候感到ruby 相當適合初學程式語言的朋友,因小弟年已近 40,
又非IT 人,也沒大學學歷,從未上過任何電腦有關的課堂,工作用計算機
的機會不多,用計算機只為好玩,所懂的都是自學,ruby 卻可使我學了一
點皮毛,以前自學shell script 看那本 Sams teach yourself shell
programing in 24 hours, 看了一年才能寫到個簡單的 script, 當然我會一
點 shell 有些優勢,但 OO(物件導向)我一點不會,從這一點來說可見ruby
對初學者相當友好,入門容易,可很快便寫到些簡單的腳本玩,對初學者相當重要.
以前在windows 下學VB, 學來學去都寫不出一點東東,搞得放棄了.
當然我寫的 ruby 很差,但CU 新開 ruby 版,還是要獻醜一下,好等其他高手
指點我們這些初學者.據CU 慣例,show 腳本要註解,雖然我的腳本寫得不好,(
列如使用兩個迭代器,兩個陣列,該可簡化加速.)但依然厚著面皮寫 comment...
後來因為想測試一下速度,發覺ruby 比bash 快很多,但我想可能用了外部程序
和管道,使bash 慢了,於是純用bash 內建指令再寫一個測試,依然是ruby 佔優,
吃了飯後再用 gawk 寫, 還是ruby 勝出,當然那個 gawk script 也沒寫好,因為
可用 while ( ( getline < "file" > 0 ) 讀入檔案,以條件附合第二個條件
時以 break 跳出迴圈結束,但我想總不會快上兩三倍吧?
總的來說,如果你第一次自學程序語言, Ruby 是個不錯的選擇.
#! /usr/bin/ruby
# $PROG: rbfree
# roustic free for cygwin re-written by ruby ,(learning , my first ruby
# script). I don't think it is oo , just look like shell script..
# but.. ruby is very interesting and is worth to learn..
#
# orginal bash script , also written by me 
#
# GNU bash, version 3.2.17(15)-release (i686-pc-cygwin)
#
# #! /bin/bash
# # $PROG: free
# # roustic free for cygwin
#
# echo -e "\ttotal\tused\tfree"
# grep -A1 '^Mem:' /proc/meminfo | while read name total used free ;
# do
# total=$(( total / 1024 / 1024 ))M
# used=$(( used / 1024 / 1024 ))M
# free=$(( free / 1024 / 1024 ))M
# echo -e "$name\t$total\t$used\t$free"
# done
#
# another version, using bash only, no external program is used
# #! /bin/bash
# # $PROG: free2
# echo -e "\ttotal\tused\tfree"
# while read name total used free
# do
# if [ "$name" = 'Mem:' ] ; then
# b=$(( $total / 1024 / 1024 ))M
# c=$(( $used / 1024 / 1024 ))M
# d=$(( $free / 1024 / 1024 ))M
# elif [ "$name" = 'Swap:' ] ; then
# b=$(( $total / 1024 / 1024 ))M
# c=$(( $used / 1024 / 1024 ))M
# d=$(( $free / 1024 / 1024 ))M
# else
# continue
# fi
# echo -e "$name\t$b\t$c\t$d"
# done < /proc/meminfo
#
# awk version, shell wrapper
#
# #! /bin/bash
# # $PROG: afree
# gawk '
# BEGIN {
# print "\t", "total\t", "used\t", "free"
# }
# {
# step = 1024 * 1024
# if ( $1 ~ /Mem:/ && NF == 4 ) {
# a = int($2 / step)
# b = int($3 / step)
# c = int($4 / step)
# print $1, "\t", a, "M\t", b, "M\t", c, "M"
# }
# }
# {
# if ( $1 ~ /Swap:/ && NF == 4 ) {
# a = int($2 / step)
# b = int($3 / step)
# c = int($4 / step)
# print $1, "\t", a, "M\t", b, "M\t", c, "M"
# }
# }' /proc/meminfo
#
# testing all scripts' speed with `time' command
#
# result: (screenshot)
# User@User-PC ~
# $ cat /proc/meminfo
# total: used: free:
# Mem: 1004535808 828497920 176037888
# Swap: 1319108608 64565248 1254543360
# MemTotal: 980992 kB
# MemFree: 171912 kB
# MemShared: 0 kB
# HighTotal: 0 kB
# HighFree: 0 kB
# LowTotal: 980992 kB
# LowFree: 171912 kB
# SwapTotal: 1288192 kB
#
#
# User@User-PC ~
# $ uname -a
# CYGWIN_NT-6.0 User-PC 1.5.24(0.156/4/2) 2007-01-31 10:57 i686 Cygwin
#
# User@User-PC ~
# $ echo ${BASH_VERSION[@]}
# 3.2.17(15)-release
#
# User@User-PC ~
# $ time rbfree
# total used free
# Mem: 958M 289M 668M
# Swap: 1258M 228M 1029M
#
# real 0m0.041s
# user 0m0.015s
# sys 0m0.000s
#
# User@User-PC ~
# $ time afree
# total used free
# Mem: 958 M 289 M 668 M
# Swap: 1258 M 228 M 1029 M
#
#
# real 0m0.114s
# user 0m0.031s
# sys 0m0.077s
#
# User@User-PC ~
# $ time free
# total used free
# Mem: 958M 290M 667M
# Swap: 1258M 228M 1029M
#
# real 0m0.141s
# user 0m0.015s
# sys 0m0.108s
#
# User@User-PC ~
# $ time free2
# total used free
# Mem: 958M 289M 668M
# Swap: 1258M 228M 1029M
#
# real 0m0.086s
# user 0m0.015s
# sys 0m0.062s
#
# User@User-PC ~
# $
#
# 以 filename 建立字符物件
filename = "/proc/meminfo"
# 打開IO 物件
file = open(filename)
# 以 file.gets 的方法(method) 逐行讟入
# 並用條件及常規表達式(regexp)測試取得要用的,並以
# spilt 的方式將元件放到陣列內 text.split(/\s+/e) 是以空格作
# 為分隔符,e 是英語系,ruby 可以處理日文,但中文尚未正式支援,
# 下一版會支援多語系
while text = file.gets do
if text =~ /^Mem:/
str = [ text.split(/\s+/e) ]
elsif text =~ /^Swap:/
str2 = [ text.split(/\s+/e) ]
end
end
# 這兩個是我寫時測試用的 , ruby 的p 命令好像用來查找
# 物件,陣列...似的,不過相當實用
# p str
# p str2
# 打印 header
print "\t", "total", "\t", "used", "\t", "free", "\n"
# ruby 獨特的迭代器方法,用來取回陣列元素,並以變數 i
# 每個訪問, 因為 i 被視為字符物件,我以 to_i 的方法
# 轉為數值物件來運算, 以 MB 數顯示打印
str.each { |i|
a = i[1].to_i / 1024 / 1024
b = i[2].to_i / 1024 / 1024
c = i[3].to_i / 1024 / 1024
print i[0],"\t", a,"M", "\t", b,"M", "\t", c,"M", "\n"
}
str2.each { |j|
a = j[1].to_i / 1024 / 1024
b = j[2].to_i / 1024 / 1024
c = j[3].to_i / 1024 / 1024
print j[0], "\t", a,"M", "\t", b,"M", "\t", c,"M", "\n"
}
# 關閉IO 物件
file.close
下面這個簡單的腳本是我用來除去上面腳本 comment , 空行等,獻醜多一遍 
#! /usr/bin/ruby
# Usage: command argument.
filename = ARGV[0]
file = open(filename)
while text = file.gets do
case text
when /^#!/
print text
when /^#/
next
when /^\n$/
next
else
print text
end
end
file.close |
|