Problem Definition
大致來說,就是輸入兩個檔案,一個stop word list,表示不想統計的單字;一個text file,統計的文本。任何以空白分隔的字元就是單字。從參數列讀入stop word list和text file的檔名,最後輸出每個單字出現的次數。
用C style寫成的Ruby
#!/usr/local/bin/ruby if ARGV.length < 2 exit end # read stop words stop = File.new(ARGV[0]) stop_words = Hash.new() while line = stop.gets for e in line.split stop_words[e] = 1 end end stop.close # read text text = File.new(ARGV[1]) count = Hash.new(0) while line = text.gets for e in line.split count[e] += 1 if stop_words[e] == nil end end text.close # dump results for key in count.keys print count[key], ' ', key, "\n" end
使用Code Block + Iterator的Ruby
這種特色是稱為Closure吧?
#!/usr/local/bin/ruby exit if ARGV.length < 2 # read stop words stop = File.new(ARGV[0]) stop_words = Hash.new() while line = stop.gets line.split.each { |e| stop_words[e] = 1} end stop.close # read text text = File.new(ARGV[1]) count = Hash.new(0) while line = text.gets line.split.each { |e| count[e] += 1 if stop_words[e] == nil } end text.close # dump results count.keys.each { |key| puts "#{count[key]} #{key}" }
結論
Ruby把一些loop的動作簡化為method call,寫起來更快,可讀性更好。效率測試上聽說不會輸Java太多,我自己的例子是差不多啦。另外內建的文件查詢程式 ri 很好用,好比可以用 man 查 C function,可以用 perldoc 查 perl ,初用的感覺比這兩者更方便,最重要的是有例子。好的文件查詢系統可以大幅增加初學者留下來的機率。
Programming Ruby 2nd裡強調,這不單單是把code block傳入callback裡,而是”互讓” ( yeild ),至於互讓隱含的好處,我還不能參透。
書裡對yeild的說明範例如下,yeild若有參數的話,就是對應到 |PARAMETER| 裡。code:
def call_block puts "Start of method" yield yield puts "End of method" end call_block { puts "In the block" }
produces:
Start of method In the block In the block End of method
沒有留言:
張貼留言