Google自訂搜尋

目前分類:Ruby教學 (15)

瀏覽方式: 標題列表 簡短摘要
感謝在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 --

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(324)

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

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(480)

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]

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(169)

在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

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

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(129)

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

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(284)

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!")

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(331)

1 2 3