Skip to content

Commit 7b60f2d

Browse files
committed
Enhanced RDoc for FileUtils
1 parent c934549 commit 7b60f2d

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

lib/fileutils.rb

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ def self.private_module_function(name) #:nodoc:
110110
end
111111

112112
#
113-
# Returns the name of the current directory.
113+
# Returns a string containing the path to the current directory:
114+
#
115+
# FileUtils.pwd # => "/rdoc/fileutils"
116+
#
117+
# FileUtils.getwd is an alias for FileUtils.pwd.
114118
#
115119
def pwd
116120
Dir.pwd
@@ -121,18 +125,36 @@ def pwd
121125
module_function :getwd
122126

123127
#
124-
# Changes the current directory to the directory +dir+.
128+
# With no block given,
129+
# changes the current directory to the directory
130+
# at the path given by +dir+; returns zero:
131+
#
132+
# FileUtils.pwd # => "/rdoc/fileutils"
133+
# FileUtils.cd('..')
134+
# FileUtils.pwd # => "/rdoc"
135+
# FileUtils.cd('fileutils')
136+
#
137+
# With a block given, changes the current directory to the directory
138+
# at the path given by +dir+, calls the block with argument +dir+,
139+
# and restores the original current directory; returns the block's value:
125140
#
126-
# If this method is called with block, resumes to the previous
127-
# working directory after the block execution has finished.
141+
# FileUtils.pwd # => "/rdoc/fileutils"
142+
# FileUtils.cd('..') { |arg| [arg, FileUtils.pwd] } # => ["..", "/rdoc"]
143+
# FileUtils.pwd # => "/rdoc/fileutils"
128144
#
129-
# FileUtils.cd('/') # change directory
145+
# Keyword arguments:
130146
#
131-
# FileUtils.cd('/', verbose: true) # change directory and report it
147+
# - <tt>verbose: true</tt> - prints an equivalent command:
132148
#
133-
# FileUtils.cd('/') do # change directory
134-
# # ... # do something
135-
# end # return to original directory
149+
# FileUtils.cd('..')
150+
# FileUtils.cd('fileutils')
151+
#
152+
# Output:
153+
#
154+
# cd ..
155+
# cd fileutils
156+
#
157+
# FileUtils.chdir is an alias for FileUtils.cd.
136158
#
137159
def cd(dir, verbose: nil, &block) # :yield: dir
138160
fu_output_message "cd #{dir}" if verbose
@@ -146,11 +168,14 @@ def cd(dir, verbose: nil, &block) # :yield: dir
146168
module_function :chdir
147169

148170
#
149-
# Returns true if +new+ is newer than all +old_list+.
150-
# Non-existent files are older than any file.
171+
# Returns +true+ if the file at path +new+
172+
# is newer than all the files at paths in array +old_list+.;
173+
# +false+ otherwise:
174+
#
175+
# FileUtils.uptodate?('Rakefile', ['Gemfile', 'README.md']) # => true
176+
# FileUtils.uptodate?('Gemfile', ['Rakefile', 'README.md']) # => false
151177
#
152-
# FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \
153-
# system 'make hello.o'
178+
# A non-existent file is considered to be infinitely old.
154179
#
155180
def uptodate?(new, old_list)
156181
return false unless File.exist?(new)
@@ -170,12 +195,32 @@ def remove_trailing_slash(dir) #:nodoc:
170195
private_module_function :remove_trailing_slash
171196

172197
#
173-
# Creates one or more directories.
198+
# Creates directories at the paths in the given +list+ (an array of strings);
199+
# returns +list+.
200+
#
201+
# With no keyword arguments, creates a directory at each +path+ in +list+
202+
# by calling: <tt>Dir.mkdir(path, mode)</tt>;
203+
# see {Dir.mkdir}[https://docs.ruby-lang.org/en/master/Dir.html#method-c-mkdir]:
204+
#
205+
# FileUtils.mkdir(%w[tmp0 tmp1]) # => ["tmp0", "tmp1"]
206+
#
207+
# Keyword arguments:
208+
#
209+
# - <tt>mode: <i>integer</i></tt> - also calls <tt>File.chmod(mode, path)</tt>;
210+
# see {File.chmod}[https://docs.ruby-lang.org/en/master/File.html#method-c-chmod].
211+
# - <tt>noop: true</tt> - does not create directories.
212+
# - <tt>verbose: true</tt> - prints an equivalent command:
213+
#
214+
# FileUtils.mkdir(%w[tmp0 tmp1], verbose: true)
215+
# FileUtils.mkdir(%w[tmp2 tmp3], mode: 0700, verbose: true)
216+
#
217+
# Output:
218+
#
219+
# mkdir tmp0 tmp1
220+
# mkdir -m 700 tmp2 tmp3
174221
#
175-
# FileUtils.mkdir 'test'
176-
# FileUtils.mkdir %w(tmp data)
177-
# FileUtils.mkdir 'notexist', noop: true # Does not really create.
178-
# FileUtils.mkdir 'tmp', mode: 0700
222+
# Raises an exception if any path in +list+ points to an existing
223+
# file or directory, or if for any reason a directory cannot be created.
179224
#
180225
def mkdir(list, mode: nil, noop: nil, verbose: nil)
181226
list = fu_list(list)

0 commit comments

Comments
 (0)