summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--lib/pathname.rb13
-rw-r--r--test/pathname/test_pathname.rb11
3 files changed, 27 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 7a4ae586a9..59587468af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Wed Feb 13 22:46:36 2008 Tanaka Akira <akr@fsij.org>
+
+ * lib/pathname.rb (Pathname#sub_ext): new method. [ruby-list:44608]
+
Wed Feb 13 21:50:32 2008 Yusuke Endoh <mame@tsg.ne.jp>
* proc.c (proc_curry): new method. [ruby-dev:33676]
diff --git a/lib/pathname.rb b/lib/pathname.rb
index e4ca5489ce..b2e119db11 100644
--- a/lib/pathname.rb
+++ b/lib/pathname.rb
@@ -255,11 +255,22 @@ 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
+ # <i>repl</i>.
+ #
+ # If self has no extension part, <i>repl</i> is appended.
+ def sub_ext(repl)
+ ext = File.extname(@path)
+ self.class.new(@path.chomp(ext) + repl)
+ end
+
# chop_basename(path) -> [pre-basename, basename] or nil
def chop_basename(path)
base = File.basename(path)
diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb
index 246b69f0e3..11c1bb7b2b 100644
--- a/test/pathname/test_pathname.rb
+++ b/test/pathname/test_pathname.rb
@@ -381,6 +381,17 @@ class TestPathname < Test::Unit::TestCase
def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
+ def pathsubext(path, repl) Pathname.new(path).sub_ext(repl).to_s end
+ defassert(:pathsubext, 'a.o', 'a.c', '.o')
+ defassert(:pathsubext, 'a.o', 'a.c++', '.o')
+ defassert(:pathsubext, 'a.png', 'a.gif', '.png')
+ defassert(:pathsubext, 'ruby.tar.bz2', 'ruby.tar.gz', '.bz2')
+ defassert(:pathsubext, 'd/a.o', 'd/a.c', '.o')
+ defassert(:pathsubext, 'foo', 'foo.exe', '')
+ defassert(:pathsubext, 'lex.yy.o', 'lex.yy.c', '.o')
+ defassert(:pathsubext, 'fooaa.o', 'fooaa', '.o')
+ defassert(:pathsubext, 'd.e/aa.o', 'd.e/aa', '.o')
+
def root?(path)
Pathname.new(path).root?
end