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

ruby-changes:41902

From: nobu <ko1@a...>
Date: Tue, 1 Mar 2016 11:25:54 +0900 (JST)
Subject: [ruby-changes:41902] nobu:r53975 (trunk): fileutils.rb: keyword arguments

nobu	2016-03-01 11:26:44 +0900 (Tue, 01 Mar 2016)

  New Revision: 53975

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53975

  Log:
    fileutils.rb: keyword arguments
    
    * lib/fileutils.rb: use keyword arguments instead of option
      hashes.

  Modified files:
    trunk/ChangeLog
    trunk/lib/fileutils.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53974)
+++ ChangeLog	(revision 53975)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Mar  1 11:25:48 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/fileutils.rb: use keyword arguments instead of option
+	  hashes.
+
 Mon Feb 29 16:50:20 2016  hanachin  <hanachin@g...>
 
 	* array.c (rb_ary_push_m): [DOC] Remove trailing comma from
Index: lib/fileutils.rb
===================================================================
--- lib/fileutils.rb	(revision 53974)
+++ lib/fileutils.rb	(revision 53975)
@@ -92,11 +92,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L92
     private_class_method name
   end
 
-  # This hash table holds command options.
-  OPT_TABLE = {}   #:nodoc: internal use only
-
-  #
-  # Options: (none)
   #
   # Returns the name of the current directory.
   #
@@ -109,8 +104,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L104
   module_function :getwd
 
   #
-  # Options: verbose
-  #
   # Changes the current directory to the directory +dir+.
   #
   # If this method is called with block, resumes to the old
@@ -122,22 +115,16 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L115
   #     [...]               # do something
   #   end                   # return to original directory
   #
-  def cd(dir, options = {}, &block) # :yield: dir
-    fu_check_options options, OPT_TABLE['cd']
-    fu_output_message "cd #{dir}" if options[:verbose]
+  def cd(dir, verbose: nil, &block) # :yield: dir
+    fu_output_message "cd #{dir}" if verbose
     Dir.chdir(dir, &block)
-    fu_output_message 'cd -' if options[:verbose] and block
+    fu_output_message 'cd -' if verbose and block
   end
   module_function :cd
 
   alias chdir cd
   module_function :chdir
 
-  OPT_TABLE['cd']    =
-  OPT_TABLE['chdir'] = [:verbose]
-
-  #
-  # Options: (none)
   #
   # Returns true if +new+ is newer than all +old_list+.
   # Non-existent files are older than any file.
@@ -163,8 +150,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L150
   private_module_function :remove_trailing_slash
 
   #
-  # Options: mode noop verbose
-  #
   # Creates one or more directories.
   #
   #   FileUtils.mkdir 'test'
@@ -172,22 +157,17 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L157
   #   FileUtils.mkdir 'notexist', :noop => true  # Does not really create.
   #   FileUtils.mkdir 'tmp', :mode => 0700
   #
-  def mkdir(list, options = {})
-    fu_check_options options, OPT_TABLE['mkdir']
+  def mkdir(list, mode: nil, noop: nil, verbose: nil)
     list = fu_list(list)
-    fu_output_message "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
-    return if options[:noop]
+    fu_output_message "mkdir #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
+    return if noop
 
     list.each do |dir|
-      fu_mkdir dir, options[:mode]
+      fu_mkdir dir, mode
     end
   end
   module_function :mkdir
 
-  OPT_TABLE['mkdir'] = [:mode, :noop, :verbose]
-
-  #
-  # Options: mode noop verbose
   #
   # Creates a directory and all its parent directories.
   # For example,
@@ -202,16 +182,15 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L182
   #
   # You can pass several directories at a time in a list.
   #
-  def mkdir_p(list, options = {})
-    fu_check_options options, OPT_TABLE['mkdir_p']
+  def mkdir_p(list, mode: nil, noop: nil, verbose: nil)
     list = fu_list(list)
-    fu_output_message "mkdir -p #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if options[:verbose]
-    return *list if options[:noop]
+    fu_output_message "mkdir -p #{mode ? ('-m %03o ' % mode) : ''}#{list.join ' '}" if verbose
+    return *list if noop
 
     list.map {|path| remove_trailing_slash(path)}.each do |path|
       # optimize for the most common case
       begin
-        fu_mkdir path, options[:mode]
+        fu_mkdir path, mode
         next
       rescue SystemCallError
         next if File.directory?(path)
@@ -224,7 +203,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L203
       end
       stack.reverse_each do |dir|
         begin
-          fu_mkdir dir, options[:mode]
+          fu_mkdir dir, mode
         rescue SystemCallError
           raise unless File.directory?(dir)
         end
@@ -240,10 +219,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L219
   module_function :mkpath
   module_function :makedirs
 
-  OPT_TABLE['mkdir_p']  =
-  OPT_TABLE['mkpath']   =
-  OPT_TABLE['makedirs'] = [:mode, :noop, :verbose]
-
   def fu_mkdir(path, mode)   #:nodoc:
     path = remove_trailing_slash(path)
     if mode
@@ -256,8 +231,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L231
   private_module_function :fu_mkdir
 
   #
-  # Options: parents, noop, verbose
-  #
   # Removes one or more directories.
   #
   #   FileUtils.rmdir 'somedir'
@@ -265,12 +238,10 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L238
   #   # Does not really remove directory; outputs message.
   #   FileUtils.rmdir 'somedir', :verbose => true, :noop => true
   #
-  def rmdir(list, options = {})
-    fu_check_options options, OPT_TABLE['rmdir']
+  def rmdir(list, parents: nil, noop: nil, verbose: nil)
     list = fu_list(list)
-    parents = options[:parents]
-    fu_output_message "rmdir #{parents ? '-p ' : ''}#{list.join ' '}" if options[:verbose]
-    return if options[:noop]
+    fu_output_message "rmdir #{parents ? '-p ' : ''}#{list.join ' '}" if verbose
+    return if noop
     list.each do |dir|
       begin
         Dir.rmdir(dir = remove_trailing_slash(dir))
@@ -286,12 +257,8 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L257
   end
   module_function :rmdir
 
-  OPT_TABLE['rmdir'] = [:parents, :noop, :verbose]
-
-  #
-  # Options: force noop verbose
   #
-  # <b><tt>ln(old, new, options = {})</tt></b>
+  # <b><tt>ln(old, new, **options)</tt></b>
   #
   # Creates a hard link +new+ which points to +old+.
   # If +new+ already exists and it is a directory, creates a link +new/old+.
@@ -301,7 +268,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L268
   #   FileUtils.ln 'gcc', 'cc', :verbose => true
   #   FileUtils.ln '/usr/bin/emacs21', '/usr/bin/emacs'
   #
-  # <b><tt>ln(list, destdir, options = {})</tt></b>
+  # <b><tt>ln(list, destdir, **options)</tt></b>
   #
   # Creates several hard links in a directory, with each one pointing to the
   # item in +list+.  If +destdir+ is not a directory, raises Errno::ENOTDIR.
@@ -310,12 +277,11 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L277
   #   cd '/sbin'
   #   FileUtils.ln %w(cp mv mkdir), '/bin'   # Now /sbin/cp and /bin/cp are linked.
   #
-  def ln(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['ln']
-    fu_output_message "ln#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
+  def ln(src, dest, force: nil, noop: nil, verbose: nil)
+    fu_output_message "ln#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_each_src_dest0(src, dest) do |s,d|
-      remove_file d, true if options[:force]
+      remove_file d, true if force
       File.link s, d
     end
   end
@@ -324,13 +290,8 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L290
   alias link ln
   module_function :link
 
-  OPT_TABLE['ln']   =
-  OPT_TABLE['link'] = [:force, :noop, :verbose]
-
-  #
-  # Options: force noop verbose
   #
-  # <b><tt>ln_s(old, new, options = {})</tt></b>
+  # <b><tt>ln_s(old, new, **options)</tt></b>
   #
   # Creates a symbolic link +new+ which points to +old+.  If +new+ already
   # exists and it is a directory, creates a symbolic link +new/old+.  If +new+
@@ -340,7 +301,7 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L301
   #   FileUtils.ln_s '/usr/bin/ruby', '/usr/local/bin/ruby'
   #   FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true
   #
-  # <b><tt>ln_s(list, destdir, options = {})</tt></b>
+  # <b><tt>ln_s(list, destdir, **options)</tt></b>
   #
   # Creates several symbolic links in a directory, with each one pointing to the
   # item in +list+.  If +destdir+ is not a directory, raises Errno::ENOTDIR.
@@ -349,12 +310,11 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L310
   #
   #   FileUtils.ln_s Dir.glob('bin/*.rb'), '/home/aamine/bin'
   #
-  def ln_s(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['ln_s']
-    fu_output_message "ln -s#{options[:force] ? 'f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
+  def ln_s(src, dest, force: nil, noop: nil, verbose: nil)
+    fu_output_message "ln -s#{force ? 'f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_each_src_dest0(src, dest) do |s,d|
-      remove_file d, true if options[:force]
+      remove_file d, true if force
       File.symlink s, d
     end
   end
@@ -363,27 +323,15 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L323
   alias symlink ln_s
   module_function :symlink
 
-  OPT_TABLE['ln_s']    =
-  OPT_TABLE['symlink'] = [:force, :noop, :verbose]
-
-  #
-  # Options: noop verbose
   #
   # Same as
   #   #ln_s(src, dest, :force => true)
   #
-  def ln_sf(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['ln_sf']
-    options = options.dup
-    options[:force] = true
-    ln_s src, dest, options
+  def ln_sf(src, dest, noop: nil, verbose: nil)
+    ln_s src, dest, force: true, noop: noop, verbose: verbose
   end
   module_function :ln_sf
 
-  OPT_TABLE['ln_sf'] = [:noop, :verbose]
-
-  #
-  # Options: preserve noop verbose
   #
   # Copies a file content +src+ to +dest+.  If +dest+ is a directory,
   # copies +src+ to +dest/src+.
@@ -395,12 +343,11 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L343
   #   FileUtils.cp %w(cgi.rb complex.rb date.rb), '/usr/lib/ruby/1.6', :verbose => true
   #   FileUtils.cp 'symlink', 'dest'   # copy content, "dest" is not a symlink
   #
-  def cp(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['cp']
-    fu_output_message "cp#{options[:preserve] ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
+  def cp(src, dest, preserve: nil, noop: nil, verbose: nil)
+    fu_output_message "cp#{preserve ? ' -p' : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_each_src_dest(src, dest) do |s, d|
-      copy_file s, d, options[:preserve]
+      copy_file s, d, preserve
     end
   end
   module_function :cp
@@ -408,11 +355,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L355
   alias copy cp
   module_function :copy
 
-  OPT_TABLE['cp']   =
-  OPT_TABLE['copy'] = [:preserve, :noop, :verbose]
-
-  #
-  # Options: preserve noop verbose dereference_root remove_destination
   #
   # Copies +src+ to +dest+. If +src+ is a directory, this method copies
   # all its contents recursively. If +dest+ is a directory, copies
@@ -434,21 +376,16 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L376
   #   FileUtils.cp_r 'src/.', 'dest'     # cp_r('src', 'dest') makes dest/src,
   #                                      # but this doesn't.
   #
-  def cp_r(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['cp_r']
-    fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
-    options = options.dup
-    options[:dereference_root] = true unless options.key?(:dereference_root)
+  def cp_r(src, dest, preserve: nil, noop: nil, verbose: nil,
+           dereference_root: true, remove_destination: nil)
+    fu_output_message "cp -r#{preserve ? 'p' : ''}#{remove_destination ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_each_src_dest(src, dest) do |s, d|
-      copy_entry s, d, options[:preserve], options[:dereference_root], options[:remove_destination]
+      copy_entry s, d, preserve, dereference_root, remove_destination
     end
   end
   module_function :cp_r
 
-  OPT_TABLE['cp_r'] = [:preserve, :noop, :verbose,
-                       :dereference_root, :remove_destination]
-
   #
   # Copies a file system entry +src+ to +dest+.
   # If +src+ is a directory, this method copies its contents recursively.
@@ -499,8 +436,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L436
   module_function :copy_stream
 
   #
-  # Options: force noop verbose
-  #
   # Moves file(s) +src+ to +dest+.  If +file+ and +dest+ exist on the different
   # disk partition, the file is copied then the original file is removed.
   #
@@ -510,10 +445,9 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L445
   #   FileUtils.mv %w(junk.txt dust.txt), '/home/aamine/.trash/'
   #   FileUtils.mv Dir.glob('test*.rb'), 'test', :noop => true, :verbose => true
   #
-  def mv(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['mv']
-    fu_output_message "mv#{options[:force] ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
+  def mv(src, dest, force: nil, noop: nil, verbose: nil, secure: nil)
+    fu_output_message "mv#{force ? ' -f' : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_each_src_dest(src, dest) do |s, d|
       destent = Entry_.new(d, nil, true)
       begin
@@ -528,14 +462,14 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L462
           File.rename s, d
         rescue Errno::EXDEV
           copy_entry s, d, true
-          if options[:secure]
-            remove_entry_secure s, options[:force]
+          if secure
+            remove_entry_secure s, force
           else
-            remove_entry s, options[:force]
+            remove_entry s, force
           end
         end
       rescue SystemCallError
-        raise unless options[:force]
+        raise unless force
       end
     end
   end
@@ -544,17 +478,12 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L478
   alias move mv
   module_function :move
 
-  OPT_TABLE['mv']   =
-  OPT_TABLE['move'] = [:force, :noop, :verbose, :secure]
-
   def rename_cannot_overwrite_file?   #:nodoc:
     /emx/ =~ RUBY_PLATFORM
   end
   private_module_function :rename_cannot_overwrite_file?
 
   #
-  # Options: force noop verbose
-  #
   # Remove file(s) specified in +list+.  This method cannot remove directories.
   # All StandardErrors are ignored when the :force option is set.
   #
@@ -562,14 +491,13 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L491
   #   FileUtils.rm Dir.glob('*.so')
   #   FileUtils.rm 'NotExistFile', :force => true   # never raises exception
   #
-  def rm(list, options = {})
-    fu_check_options options, OPT_TABLE['rm']
+  def rm(list, force: nil, noop: nil, verbose: nil)
     list = fu_list(list)
-    fu_output_message "rm#{options[:force] ? ' -f' : ''} #{list.join ' '}" if options[:verbose]
-    return if options[:noop]
+    fu_output_message "rm#{force ? ' -f' : ''} #{list.join ' '}" if verbose
+    return if noop
 
     list.each do |path|
-      remove_file path, options[:force]
+      remove_file path, force
     end
   end
   module_function :rm
@@ -577,32 +505,19 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L505
   alias remove rm
   module_function :remove
 
-  OPT_TABLE['rm']     =
-  OPT_TABLE['remove'] = [:force, :noop, :verbose]
-
-  #
-  # Options: noop verbose
   #
   # Equivalent to
   #
   #   #rm(list, :force => true)
   #
-  def rm_f(list, options = {})
-    fu_check_options options, OPT_TABLE['rm_f']
-    options = options.dup
-    options[:force] = true
-    rm list, options
+  def rm_f(list, noop: nil, verbose: nil)
+    rm list, force: true, noop: noop, verbose: verbose
   end
   module_function :rm_f
 
   alias safe_unlink rm_f
   module_function :safe_unlink
 
-  OPT_TABLE['rm_f']        =
-  OPT_TABLE['safe_unlink'] = [:noop, :verbose]
-
-  #
-  # Options: force noop verbose secure
   #
   # remove files +list+[0] +list+[1]... If +list+[n] is a directory,
   # removes its all contents recursively. This method ignores
@@ -622,26 +537,20 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L537
   # NOTE: This method calls #remove_entry_secure if :secure option is set.
   # See also #remove_entry_secure.
   #
-  def rm_r(list, options = {})
-    fu_check_options options, OPT_TABLE['rm_r']
-    # options[:secure] = true unless options.key?(:secure)
+  def rm_r(list, force: nil, noop: nil, verbose: nil, secure: nil)
     list = fu_list(list)
-    fu_output_message "rm -r#{options[:force] ? 'f' : ''} #{list.join ' '}" if options[:verbose]
-    return if options[:noop]
+    fu_output_message "rm -r#{force ? 'f' : ''} #{list.join ' '}" if verbose
+    return if noop
     list.each do |path|
-      if options[:secure]
-        remove_entry_secure path, options[:force]
+      if secure
+        remove_entry_secure path, force
       else
-        remove_entry path, options[:force]
+        remove_entry path, force
       end
     end
   end
   module_function :rm_r
 
-  OPT_TABLE['rm_r'] = [:force, :noop, :verbose, :secure]
-
-  #
-  # Options: noop verbose secure
   #
   # Equivalent to
   #
@@ -650,20 +559,14 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L559
   # WARNING: This method causes local vulnerability.
   # Read the documentation of #rm_r first.
   #
-  def rm_rf(list, options = {})
-    fu_check_options options, OPT_TABLE['rm_rf']
-    options = options.dup
-    options[:force] = true
-    rm_r list, options
+  def rm_rf(list, noop: nil, verbose: nil, secure: nil)
+    rm_r list, force: true, noop: noop, verbose: verbose, secure: secure
   end
   module_function :rm_rf
 
   alias rmtree rm_rf
   module_function :rmtree
 
-  OPT_TABLE['rm_rf']  =
-  OPT_TABLE['rmtree'] = [:noop, :verbose, :secure]
-
   #
   # This method removes a file system entry +path+.  +path+ shall be a
   # regular file, a directory, or something.  If +path+ is a directory,
@@ -844,8 +747,6 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L747
   module_function :compare_stream
 
   #
-  # Options: mode preserve noop verbose
-  #
   # If +src+ is not same as +dest+, copies it and changes the permission
   # mode to +mode+.  If +dest+ is a directory, destination is +dest+/+src+.
   # This method removes destination before copy.
@@ -853,24 +754,21 @@ module FileUtils https://github.com/ruby/ruby/blob/trunk/lib/fileutils.rb#L754
   #   FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
   #   FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
   #
-  def install(src, dest, options = {})
-    fu_check_options options, OPT_TABLE['install']
-    fu_output_message "install -c#{options[:preserve] && ' -p'}#{options[:mode] ? (' -m 0%o' % options[:mode]) : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
-    return if options[:noop]
+  def install(src, dest, mode: nil, preserve: nil, noop: nil, verbose: nil)
+    fu_output_message "install -c#{preserve && ' -p'}#{mode ? (' -m 0%o' % mode) : ''} #{[src,dest].flatten.join ' '}" if verbose
+    return if noop
     fu_ (... truncated)

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

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