Google自訂搜尋

目前分類:Ruby教學 (16)

瀏覽方式: 標題列表 簡短摘要

大家應該都知道我又重新入手了Macbook

實在是忘不了那種感覺啊!

而且XCode + Interface Builder根本就是絕配!!

用Ruby寫OSX程式也不會覺得討厭,所以..

 

Leopard中內建了Ruby/Rails

請記得移除並且自己編譯安裝Ruby

然後在編譯Ruby時記得:

./configure --enable-shared --enable-pthread

這樣才能另外去編譯Ruby Cocoa,不然會炸這個訊息出來:ERROR: ruby must be built as a shared library

至於pthread.. 編譯tk會用到:P

就這樣了,只是take a note而已

hechian 發表在 痞客邦 留言(0) 人氣()

感謝在Ruby-talk上的:
Chris Carter
David A. Black
Harry
Robert Dober
James Edward
:)
原本的程式碼太長,而且使用內建的功能組合起來就好
再者,原本的程式會把陣列的元素強制轉型為String

新的程式碼為:
class Array
  def longest
    # Harry <http://www.kakueki.com/ruby/list.html>
    self.select{|r| r.to_s.size == self.max{|x, y| x.to_s.size <=> y.to_s.size}.to_s.size}
  end
end
這個程式是由Harry所寫出的,底下轉貼原文:

On 4/29/07, Billy Hsu <ruby.maillist@gmail.com> wrote:
> Thanks for your reply, I learned more on this thread :P
> But I have a question:
> If I have an array contain:
>   ary = [1, 12, 234, "456"]
> there has two elements which size is 3, but the longest method just returned
> one of them.
> I can't solve it :(
>

Is this what you are looking for?
Do you want all longest elements?

big = [1, 12, 234,45,978, "456"].max {|x,y| x.to_s.size <=> y.to_s.size}
p [1, 12, 234,45,978, "456"].select {|r| r.to_s.size == big.to_s.size}
由於Harry不喜歡被張貼信箱,因此我將他的網站給貼上來:
http://www.kakueki.com/ruby/list.html
A Look into Japanese Ruby List in English

再一次謝謝Harry的幫助:)
也謝謝其他人,讓我學到許多東西:D
Thanks again and again!!

CFC --

hechian 發表在 痞客邦 留言(0) 人氣()

這是http://www.ruby-lang.org/zh_TW/ 站長所寫的一篇教學
網址是:http://info.sayya.org/~sjh/sjh_rubygtk.pdf
寫得很詳細、簡單明瞭!
如果有需要可以看看

hechian 發表在 痞客邦 留言(0) 人氣()

XML文件我是用ReXML啦.. 不過我這邊不是要介紹ReXML,是要來介紹hpricot這個Library的
安裝方式:
gem install hpricot
or
gem install hpricot --source http://code.whytheluckystiff.net

第一個會連線到gem server去抓來裝,不會有最新的更新;第二個會連到指定的gem server,那邊更新速度較快,我還看到jruby版本的gem..

OK,廢話不多說,趕緊來看看
官方網站是:http://code.whytheluckystiff.net/hpricot/

如果會jQuery的人,這個是用jQuery當底層的喔!
我來個例子吧

require 'rubygems'
require 'hpricot'
require 'open-uri'
doc = Hpricot(open("http://article.zuso.org.tw/show.php?id=1453"))
tb = doc.search("//table")
puts "Tables: #{tb.size}"
puts tb[0]

hechian 發表在 痞客邦 留言(0) 人氣()

在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

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

hechian 發表在 痞客邦 留言(0) 人氣()

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,可以同步變更檔案內容(也就是說當我使用這個網頁版編輯器時,我在一般文字編輯器上的修改,這個編輯器也可以即時更新)!

hechian 發表在 痞客邦 留言(0) 人氣()

require 'Win32API'

=begin
  Message Box:
  Coded by CFC <at> Zuso Security
  CFC <zusocfc@gmail.com>
  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!")

hechian 發表在 痞客邦 留言(0) 人氣()

研究了一下.. 寫出來當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

hechian 發表在 痞客邦 留言(0) 人氣()

這是我自己寫的Ruby教學..
希望各位前輩們不吝指教!
也歡迎新朋友們參考討論:)

網址是:

http://zusocfc.pbwiki.com/Ruby%E6%95%99%E5%AD%B8%E7%B3%BB%E5%88%97

如果各位希望我寫什麼範例的話.. 可以留言在Comments內(上面有連結),我會新增在範例裡面的
要是可以的話.. 也希望可以提供其他語言寫的範例(最好是VB或者C、Java跟Perl),我可以改寫成Ruby的給各位看看

hechian 發表在 痞客邦 留言(0) 人氣()

搭配物件導向@@
觀看可讀性高的原始碼請按此

class Stack

  def initialize ary=0, max=0, lvl=0
    @ary = ary
    @max = max
    @lvl = lvl
  end
 
  def add
    if @lvl < @max
      puts "Please input a value for add to the stack!"
      @ary[@lvl] = self.get_value
      @lvl+=1
      puts "Now you have #{@lvl} elements for the array!"
    else
      puts "Stack is full!"
    end
  end
 
  def del
    puts "#{@ary[@lvl-1]} has been removed!"
    @lvl -= 1
    if @lvl <= 0
      puts "You have no element in the array!"
    else
      puts "Now you have #{@lvl} elements in the array!"
    end
  end
 
  def get_value
    return gets.chomp
  end
 
  def list
 
    if @lvl <= 0
      puts "You have no element in the array!"
    else
      for ele in 0 ... @lvl
        puts @ary[ele]
      end
    end
    true
  end

  public :initialize, :add, :del, :list
  protected :get_value
end

puts "Please input a number for the stack max!"
ary = {}
max = gets.chomp.to_i
lvl = 0

stack = Stack.new(ary, max, lvl)

=begin

Actions:
  1. add -- for add elements
  2. del -- for remove elements
  3. list -- for list elements
  4. exit -- end this program
=end


while true
  puts "Please select a action!"
  puts <Actions:
  1. add -- for add elements
  2. del -- for remove elements
  3. list -- for list elements
  4. exit -- end this program
EOF
 
 
  action = gets.chomp
 
  case action
    when "add" ,"1"
      puts "Adding array element!"
      stack.add
    when "del", "2"
      puts "Removing array element!"
      stack.del
    when "list", "3"
      puts "Listing array elements!"
      stack.list
    when "exit", "4"
      puts "Bye~ bye!"
      exit
  end
end


hechian 發表在 痞客邦 留言(0) 人氣()

看可讀性高的程式碼請按此

程式碼有點亂,可是跟C++比起來我覺得很簡單了= =

def stack(ary, lvl, max, input)
if lvl < max
ary[lvl] = input
puts "#{input} has been added in the stack!"
lvl+=1
get_element ary
else
puts "Stack is full!!!!"
exit
end

lvl
end

def get_element ary
print "Element in the array:"
ary.each do |x|
print "#{x} "
end
puts
end


puts "Please input a number for the stack max!"
max=gets.to_i
lvl=0
ary={}
puts "Please input elements into the stack, type exit to exit."
arg = gets.chomp
while arg != "exit"
lvl=stack(ary, lvl, max, arg)
arg = gets.chomp
end


目前只有新增的版本
至於刪除嘛... Orz..
還沒有寫@@


hechian 發表在 痞客邦 留言(0) 人氣()

這篇不知道該分類到XML還是Ruby教學.. 不過因為是講Ruby搭配REXML.. 應該是Ruby教學吧??
由於王宗松老師的案子,讓我興起了用Ruby寫個XML公佈欄的念頭
用Ruby + REXML可以很輕鬆的Parsing XML!搭配XML Builder則可以非常容易的寫XML!
OK,閒話休提.. 目前檔案應該是這樣的...

XML_BB/
  ├ functions/
  │  ├ include.rb
  │  └ funcs.rb
  ├ list_news.rb
  └ news.rb

檔案目前是只有這些拉!
其中include.rb裡面是引入檔案:
  $KCODE = "UTF-8"
  require 'rexml/document'
  require 'jcode'
  include REXML


funcs.rb裡面:
  def load_file(file)
    return Document.new(File.new(file))
  end
 
  def set_path(path)
    return path
  end

  def get_elements_num(doc, path)
    return XPath.match(doc, path).size
  end

  def show_author(nme_hash, news, path, num)
    print nme_hash["author"] + ":"
    puts news.elements["#{path}item[#{num}]/author"].attributes["name"]
    puts "====="
  end

暫時寫到這邊,等程式完成之後再PO其他檔案的原始碼
這個版本暫時不支援RSS,未來會推出支援RSS 2.0標準的XMLBB

hechian 發表在 痞客邦 留言(0) 人氣()

一直測試一直測試
終於有點結果了
用rss.each_element("/rss/channel/item[1]/*"){ |elm|
  p elm.name
}
這樣可以印出channel下第一個item的所有子標籤名稱
用這個可以改寫我之前無名小站RSS Reader PHP版
PHP的XML Parser不是很好用.. 可是Ruby的ReXML是一個非常好的工具
hmmm.. ZenixD也被嚇到了XD
我想很快就會有一個Ruby版的XML公告欄!

hechian 發表在 痞客邦 留言(0) 人氣()

嗯 ... 忙忙忙
早知道就不裝1.8.4 Preview 3的Windows One Click了=  =
裝下去問題一堆
不是找不到Tk,就是GTK裝不好
~"~...
請各位下載1.8.2的版本
http://rubyforge.org/frs/?group_id=167

安裝^^

Then,下載Ruby/GTK2 Binary for Windows
http://prdownloads.sourceforge.net/ruby-gnome2/ruby-gtk2-0.14.1-1-i386-msvcrt-1.zip?download

接著,解壓縮到一個資料夾
再進去console模式(CMD or command)
然後cd到解壓縮後的資料夾,輸入ruby install.rb
裝好之後,請下載Download gtk+-win32-runtime-2.8
到這裡找找GLADE for Windows

然後安裝^^
裝好後,重新再開一個Console視窗,輸入ruby -v
然後,輸入ruby -e "require 'gtk2'"
按下Enter看有沒有出現錯誤訊息,沒有就表示你成功拉\^o^/

hechian 發表在 痞客邦 留言(0) 人氣()

BEGIN    class    ensure   nil      self     when
END def false not super while
alias defined? for or then yield
and do if redo true
begin else in rescue undef
break elsif module retry unless
case end next return until
 

hechian 發表在 痞客邦 留言(0) 人氣()

Ruby的迴圈還真多寫法阿..
底下是迴圈的用法(只是簡介)

//↓↓↓淺顯易懂↓↓↓
次數.times do
 #敘述
end

//↓↓↓類似 for 變數 = 初始值 to 結尾值 step 1↓↓↓
初始值.upto(結尾值) do |變數|
 #敘述
end
//↓↓↓類似 for 變數 = 初始值 to 結尾值 step -1↓↓↓
初始值.downto(結尾值) do |變數|
 #敘述
end

//↓↓↓類似 for 變數 = 初始值 to 結尾值 step 間隔數↓↓↓
初始值.step(結尾值, 間隔數) {|變數| 敘述}

//用過foreach嗎?底下就差不多那樣拉
陣列.each {|變數| 敘述}

//跟for很像拉..

/*
for 變數 = 初始值 to 結尾值
next
*/
for 變數 in 初始值..結尾值
 敘述
end

/*
for 變數 = 初始值 to 結尾值 - 1
next
*/
for 變數 in 初始值...結尾值
 敘述
end

/*
跟foreach有點像
*/
for 變數 in 陣列
 敘述
end

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html
↑這邊有詳盡介紹.. 我目前懶得寫了=  =
回家再寫吧Orz..


hechian 發表在 痞客邦 留言(0) 人氣()