對抗垃圾信!請您點這裡:

我的E-mail:
我的Skype:My status

2007年2月27日

Wikibooks上的中文版Programming Ruby

網址是:http://zh.wikibooks.org/w/index.php?title=Programming:Ruby&variant=zh
現在是我跟石頭閒語的Rock一起翻譯撰寫,如果有人有興趣的話歡迎加入翻譯的工作
請將原文的文章引用後張貼,然後修改翻譯即可
感謝您的付出!謝謝!

File讀取方式:readlines好還是while迴圈好?

在Ruby中,File可以用readlines跟跑while迴圈來讀
在這個例子中,程式p1用的是while迴圈,p2用的是readlines
執行後,秒數分別是
P1:
121.468秒
P2:
122.172秒
範例文字檔大小是:
4.07 MB (4,272,336 位元組)

範例程式碼是:

puts "P1 start"
p1_start = Time.now
open("C:/words.txt"){ |f|
while a = f.gets
print a
end
}
p1_end = Time.now
puts "P1 end"
puts "P2 start"
p2_start = Time.now
File.open("C:/words.txt") do |f|
puts f.readlines
end
p2_end = Time.now
puts "P2 end"
puts
puts "P1: ", p1_end - p1_start
puts "P2: ", p2_end - p2_start

由此可見,while快上不到一秒,但是如果在讀取大檔案的時候,用while反而會比較快
相對的,如果不考慮效率,我還是建議使用readlines

不過這只是個人看法,希望其他前輩不吝指教,謝謝!

2007年2月25日

小水滴測試用文章

有看到右邊多了什麼嗎?
沒錯!就是部落格寵物!
還蠻可愛的說A_A

HEMiDEMi文章產生器 -- Ver. 0.2

此版本尚未支援 影片代碼 功能

原始碼如下:
require 'rubygems'
require 'mechanize'

class HEMiDEMi
def initialize(username="", password="")
agent = WWW::Mechanize.new{}
f = agent.get("http://www.hemidemi.com/member/signin_form").forms[1]
f["member[username]"], f["member[password]"] = username, password
f.submit
@agent = agent
@base_url = "http://www.hemidemi.com"
@new_path = "/user_bookmark/new"
end

def run(file)
File.open(file) do |f|
f.readlines.each do |frl|
next if frl[0].chr == "#"
title, url, quotes, description, t_s, g_s = frl.split(/\t/)
self.add({
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => description,
"tag_string" => t_s,
"group_string" => g_s
})
end
end
end

def add(qs)
f = @agent.get(@base_url+@new_path).forms[1]
qs.each do |k, v|
puts "Add: #{k} as #{v}"
f["user_bookmark[#{k}]"] = v
end
f.fields.each do |x| puts x.name + " => " + x.value end
f.submit
end

end
puts "請輸入HEMiDEMi使用者帳號跟密碼(用空格分開):"
ud = gets.chomp.split(/ /)
hemidemi = HEMiDEMi.new(ud[0], ud[1])
puts "請輸入檔案位置,不輸入則預設值為當前目錄下的bms.txt:"
path = gets.chomp
path == "" ? hemidemi.run("bms.txt") : hemidemi.run(path)

另外,bms.txt檔案內容是:
# 欄位說明:
# title\turl\tquotes\tdescription\ttag_string\tgroup_string
# 標題網址引述說明標籤群組
# :按一次Tab鍵
# 底下是範例:
HEMiDEMi http://www.hemidemi.com HEMiDEMi共享書籤 hemidemi test ruby_and_ror

說明:

請先建立好bms.txt或者其他檔案名稱的純文字檔
欄位寫的很清楚,就是:標題網址引述說明標籤群組
請記得一定要用tab來分隔,建議用Windows內建的記事本來編寫
如果想要空掉某些欄位,則該欄位不填直接按下tab鍵即可,看範例就可以知道 (範例沒有寫上引述)


程式授權:MIT
程式所需套件:rubygems、mechanize跟mechanize之所需套件
mechanize安裝方式:
請先確定有rubygems,如果沒有請到RubyForge下載安裝
打開命令提示字元或者終端機,輸入:
gem i mechanize -y
如果有要你選版本的話,Windows使用者請選擇(mswin32)版本,而其他作業系統請選(ruby)版本
程式原始碼下載:
Windows (Big5)
Linux (UTF-8)

HEMiDEMi文章產生器 -- Ver. 0.1

原始碼在這邊:

require 'rubygems'
require 'mechanize'

class HEMiDEMi
def initialize(username="", password="")
agent = WWW::Mechanize.new
f = agent.get("http://www.hemidemi.com/member/signin_form").forms[1]
f["member[username]"], f["member[password]"] = username, password
f.submit
@agent = agent
@base_url = "http://www.hemidemi.com"
@new_path = "/user_bookmark/new"
end

def add(qs)
f = @agent.get(@base_url+@new_path).forms[1]
qs.each do |k, v|
f["user_bookmark[#{k}]"] = v
end
f.submit
end

end
=begin
qs = {
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => desc,
"tag_string" => t_s,
"group_string" => g_s,
"embed" => embed
}
=end

qs = {
"title" => "G00g13",
"url" => "google.com"
}

hemidemi = HEMiDEMi.new("", "").add(qs)

目前這個版本只是雛型.. 還不能夠用來大量新增!
簡單介紹一下...

qs = {
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => desc,
"tag_string" => t_s,
"group_string" => g_s,
"embed" => embed
}

qs是一個Hash,我只講其中幾個
quotes:代表的是"引述"
description:代表的是"說明"
embed:代表的是影片代碼

HEMiDEMi文章產生器.. Memo

原始碼在這邊:

require 'rubygems'
require 'mechanize'

class HEMiDEMi
def initialize(username="", password="")
agent = WWW::Mechanize.new
f = agent.get("http://www.hemidemi.com/member/signin_form").forms[1]
f["member[username]"], f["member[password]"] = username, password
f.submit
@agent = agent
@base_url = "http://www.hemidemi.com"
@new_path = "/user_bookmark/new"
end

def add(qs)
f = @agent.get(@base_url+@new_path).forms[1]
qs.each do |k, v|
f["user_bookmark[#{k}]"] = v
end
f.submit
end

end
=begin
qs = {
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => desc,
"tag_string" => t_s,
"group_string" => g_s,
"embed" => embed
}
=end

qs = {
"title" => "G00g13",
"url" => "google.com"
}

hemidemi = HEMiDEMi.new("", "").add(qs)

目前這個版本只是雛型.. 還不能夠用來大量新增!
簡單介紹一下...

qs = {
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => desc,
"tag_string" => t_s,
"group_string" => g_s,
"embed" => embed
}

qs是一個Hash,我只講其中幾個
quotes:代表的是"引述"
description:代表的是"說明"
embed:代表的是影片代碼

2007年2月24日

三場關於Ruby在Google的演講

錄影放在Google Video
議題分別是:
How to Design A Domain Specific Language -- David Pollak
Code Generation With Ruby -- Jack Herrington
Ruby and Google Maps -- Andre Lewis

正在看Ruby and Google Maps:P

51個Rails影片跟展示

網址是:http://www.bestechvideos.com/category/development/ruby/

這邊有51個不同的展示影片,也有介紹影片,非常值得去看看

社群網站建置中

現在改用Joomla,不用Xoops了
不過還在建置.. Joomla裝起來感覺還蠻好的
版面也很漂亮,用起來很舒服
主機由 Zuso Security 提供,感謝負責人 ik

目前網址是:http://willh.org/cfc/
而原本的:http://www.ruby.oss.tw則會自動轉至新網址
感謝http://twpug.net的站長Kiang提供ruby.oss.tw網域名稱及空間

等到網站建置結束,將會在Group上公告

現在.. 凌晨四點多= =
會不會被老婆罵死阿~"~a

2007年2月21日

台灣Ruby官方網站出來了!

這真是令人振奮的消息阿
雖然已經超過兩三個月了...
由SJH於2006年10月4號成立
擷圖:http://farm1.static.flickr.com/175/396720833_599f58f8e6_o_d.jpg

真是太開心了>///<

2007年2月20日

Gyre -- 優秀又強大的網頁版Rails編輯器

Gyre是一個網頁版的Rails Editor,雖然是網頁版的可是別小看它喔!
它的執行環境不過就只是從Desktop換到Web而已,一般IDE該有的功能都有喔!
Ruby Inside上有介紹:http://www.rubyinside.com/gyre-web-based-ide-and-debugger-for-rails-383.html
官方網站:http://gyre.bitscribe.net/
ScreenCast:http://gyre.bitscribe.net/screencast
我很推薦各位去看他的ScreenCast,前面介紹蠻多的.. 可以跳過不聽XD
它可以Debug,可以同步變更檔案內容(也就是說當我使用這個網頁版編輯器時,我在一般文字編輯器上的修改,這個編輯器也可以即時更新)!

Win32API Sample: MessageBox

require 'Win32API'

=begin
Message Box:
Coded by CFC Zuso Security
CFC
2007/2/16
=end

class Msgbox
def initialize(lpText="", lpCaption="", wType = 0)
Win32API.new('user32', 'MessageBox', %w(p p p i), 'i').call(0,lpText,lpCaption,wType)
end
end

def Msgbox(lpText="", lpCaption="", wType = 0)
Win32API.new('user32', 'MessageBox', %w(p p p i), 'i').call(0,lpText,lpCaption,wType)
end

採用MIT授權條款
Usage:
Msgbox.new("Hi", "Hello, world")
Msgbox.new("XD", "Hello!", 1)
Msgbox("Hi", "Hello!")

如何透過Ruby呼叫Win32API

研究了一下.. 寫出來當Memo..
我先以一個簡單的例子來解說,就用大家最愛的MessageBox吧!

require 'win32api'
msgbox = Win32API.new('user32', 'MessageBox', %w(p p p i), 'i')
msgbox.call(0, "Message body", "Messagebox title", 1) # hwnd, lpText, lpCaption, wType => Come from API Viewer (API檢視員)

OK.. 開始來講解
1. require 'win32api'
這是將win32api.rb給引入的意思,跟C的#include用意相同

2. msgbox = Win32API.new('user32', 'MessageBox', %w(p p p i), 'i')
實體化一個Win32API物件,第一個是dllname,第二個是要呼叫的東西,第三個是API參數傳遞時的資料型態,第四個是要回傳的資料型態
如果保持nil(也就是null)的話,就表示不會有傳入或回傳

3. msgbox.call(0, "Message body", "Messagebox title", 1)
透過這個物件呼叫,hwnd傳入0,lpText傳入"Message body"接著以此類推

我來張貼一下常數表

# type flag
MB_OK = 0
MB_OKCANCEL = 1
MB_ABORTRETRYIGNORE = 2
MB_YESNOCANCEL = 3
MB_YESNO = 4
MB_RETRYCANCEL = 5

# return values
IDOK = 1
IDCANCEL = 2
IDABORT = 3
IDRETRY = 4
IDIGNORE = 5
IDYES = 6
IDNO = 7