[前][次][番号順一覧][スレッド一覧]

ruby-changes:3971

From: ko1@a...
Date: Wed, 13 Feb 2008 22:50:54 +0900 (JST)
Subject: [ruby-changes:3971] akr - Ruby:r15461 (trunk): * lib/pathname.rb (Pathname#sub_ext): new method.

akr	2008-02-13 22:50:31 +0900 (Wed, 13 Feb 2008)

  New Revision: 15461

  Modified files:
    trunk/ChangeLog
    trunk/lib/pathname.rb
    trunk/test/pathname/test_pathname.rb

  Log:
    * lib/pathname.rb (Pathname#sub_ext): new method.  [ruby-list:44608]


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/pathname/test_pathname.rb?r1=15461&r2=15460&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15461&r2=15460&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/pathname.rb?r1=15461&r2=15460&diff_format=u

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15460)
+++ ChangeLog	(revision 15461)
@@ -1,3 +1,7 @@
+Wed Feb 13 22:46:36 2008  Tanaka Akira  <akr@f...>
+
+	* lib/pathname.rb (Pathname#sub_ext): new method.  [ruby-list:44608]
+
 Wed Feb 13 21:50:32 2008  Yusuke Endoh  <mame@t...>
 
 	* proc.c (proc_curry): new method. [ruby-dev:33676]
Index: lib/pathname.rb
===================================================================
--- lib/pathname.rb	(revision 15460)
+++ lib/pathname.rb	(revision 15461)
@@ -255,11 +255,22 @@
   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)
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 15460)
+++ test/pathname/test_pathname.rb	(revision 15461)
@@ -381,6 +381,17 @@
   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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]