From: Tanaka Akira Date: 2008-02-12T16:13:22+09:00 Subject: [ruby-list:44614] Re: Pathname で拡張子を変更する In article <47AFF439.1080708@moji.gr.jp>, "5.5" <5.5@moji.gr.jp> writes: > 正規表現リテラルのところで一瞬考えちゃいますが,メソッド > 一つで書けて簡潔ですね。早速使わせていただこうと思います。 > るびきちさん,ありがとうござました。 > > ※やっぱり専用メソッドはほしいですが。 こんなところですかねぇ。 Index: lib/pathname.rb =================================================================== --- lib/pathname.rb (revision 15443) +++ lib/pathname.rb (working copy) @@ -255,11 +255,19 @@ class Pathname end if File::ALT_SEPARATOR - SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/ + SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}" + SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/ else + SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}" SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/ end + # Return a pathname which the extention of the basename is substituted by + # repl. + def sub_ext(repl) + self.class.new(@path.sub(%r{\.[^#{SEPARATOR_LIST}.]*\z}o) { repl }) + end + # chop_basename(path) -> [pre-basename, basename] or nil def chop_basename(path) base = File.basename(path) -- [田中 哲][たなか あきら][Tanaka Akira]