どシロウトの後藤です. 

python のWWWライブラリを ruby にチマチマ移そうとしてます. 
とりあえず練習に URL のエスケープ関数を書いてみたので, 
誰か添削してもらえませんか? 40行ほどなので添付しました. 
# なんかダサいような気がしてます. 特に, unquote
Uppercase などはどこかで定義されるかと思ったのですが
見つけきれませんでした. 

それと質問なのですが近い将来この urllib.rb に
  p = URL.open("http://www.caelum.co.jp/~matz/ruby/)
  page = p.readlines()
  p.close()
のようなのも付け加えたいのですが, その場合, 
この場合 URL は module でなくて class にすべきなのでしょうか?
URL.quote しか使わないかも知れないのにクラスが初期化されるのは
モッタイナイので module にしようかと思っているのです. 
が, それは勘違いのような気がしてるので. 

- -- beg urllib.rb
# usage:
#   URL.quote(str, *extra)     # return escaped URL String
#   URL.unquote(str)           # return decoded URL String
module URL
  Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  Lowercase = "abcdefghijklmnopqrstuvwxyz"
  Letter = Uppercase + Lowercase
  Digits = "0123456789"
  Alwaysafe = Letter + Digits + '_,.-'
  def quote(s, *extra)
    safe = Alwaysafe
    safe += extra.join if extra
    res = []
    s.each_byte{|b|
      c = sprintf("%c",b)
      if safe.index(c)
	res << "" + c + ""
      else
	res << "%" + sprintf("%2X",b) + ""
      end
    }
    return res.join
  end
  module_function :quote
  
  Quoteprog = Regexp.compile(/%[0-9a-fA-F][0-9a-fA-F]/)
  def unquote(s)
    i = 0
    n = s.length
    res = []
    while 0 <= i and i < n
      j = (Quoteprog =~ s[i..n])
      unless j
	res << s[i..n]
	break
      else
	res << s[i,j] + s[i+j+1,2].hex.chr
	i += j+3
      end
    end
    return res.join
  end
  module_function :unquote
end
- -- end urllib.rb

--
後藤 謙太郎@北海道大学大学院理学研究科数学専攻博士課程
mailto:gotoken / math.hokudai.ac.jp