Pathname で遊んでみる

by tanabe on July 25, 2006

Lessons from Hpricot のエントリを読んで抑え切れなくなって遊んでみる。

require 'pathname'
class Pathname
  def /(path)
    self + path
  end
end

def method_missing(name, *args)
  name.to_s
end

実行結果はこんなかんじ。

root = Pathname.new '/tmp'

root/foo/bar
# => #<Pathname:/tmp/foo/bar>

(root/bar/foo).parent.children
# => [#<Pathname:/tmp/foo/bar>, #<Pathname:/tmp/foo/baz>]

もう最高。