Skip to content

Commit 39772bc

Browse files
Enhanced RDoc for #cp_lr (#71)
1 parent 79fc67f commit 39772bc

File tree

1 file changed

+66
-18
lines changed

1 file changed

+66
-18
lines changed

lib/fileutils.rb

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def rmdir(list, parents: nil, noop: nil, verbose: nil)
357357
end
358358
module_function :rmdir
359359

360+
# Creates {hard links}[https://en.wikipedia.org/wiki/Hard_link].
360361
#
361362
# When +src+ is the path to an existing file
362363
# and +dest+ is the path to a non-existent file,
@@ -369,7 +370,7 @@ def rmdir(list, parents: nil, noop: nil, verbose: nil)
369370
#
370371
# When +src+ is the path to an existing file
371372
# and +dest+ is the path to an existing directory,
372-
# creates a hard link in +dest+ pointing to +src+; returns zero:
373+
# creates a hard link at <tt>dest/src</tt> pointing to +src+; returns zero:
373374
#
374375
# Dir.children('tmp2') # => ["t.dat"]
375376
# Dir.children('tmp3') # => []
@@ -379,7 +380,7 @@ def rmdir(list, parents: nil, noop: nil, verbose: nil)
379380
# When +src+ is an array of paths to existing files
380381
# and +dest+ is the path to an existing directory,
381382
# then for each path +target+ in +src+,
382-
# creates a hard link in +dest+ pointing to +target+;
383+
# creates a hard link at <tt>dest/target</tt> pointing to +target+;
383384
# returns +src+:
384385
#
385386
# Dir.children('tmp4/') # => []
@@ -420,28 +421,75 @@ def ln(src, dest, force: nil, noop: nil, verbose: nil)
420421
alias link ln
421422
module_function :link
422423

424+
# Creates {hard links}[https://en.wikipedia.org/wiki/Hard_link].
425+
#
426+
# If +src+ is the path to a directory and +dest+ does not exist,
427+
# creates links +dest+ and descendents pointing to +src+ and its descendents:
428+
#
429+
# Dir.glob('**/*.txt')
430+
# # => ["tmp0/tmp2/t0.txt",
431+
# "tmp0/tmp2/t1.txt",
432+
# "tmp0/tmp3/t2.txt",
433+
# "tmp0/tmp3/t3.txt"]
434+
# FileUtils.cp_lr('tmp0', 'tmp1')
435+
# Dir.glob('**/*.txt')
436+
# # => ["tmp0/tmp2/t0.txt",
437+
# "tmp0/tmp2/t1.txt",
438+
# "tmp0/tmp3/t2.txt",
439+
# "tmp0/tmp3/t3.txt",
440+
# "tmp1/tmp2/t0.txt",
441+
# "tmp1/tmp2/t1.txt",
442+
# "tmp1/tmp3/t2.txt",
443+
# "tmp1/tmp3/t3.txt"]
444+
#
445+
# If +src+ is an array of paths to files and +dest+ is the path to a directory,
446+
# for each path +filepath+ in +src+, creates a link at <tt>dest/filepath</tt>
447+
# pointing to that path:
448+
#
449+
# FileUtils.rm_r('tmp1')
450+
# Dir.mkdir('tmp1')
451+
# FileUtils.cp_lr(['tmp0/tmp3/t2.txt', 'tmp0/tmp3/t3.txt'], 'tmp1')
452+
# Dir.glob('**/*.txt')
453+
# # => ["tmp0/tmp2/t0.txt",
454+
# "tmp0/tmp2/t1.txt",
455+
# "tmp0/tmp3/t2.txt",
456+
# "tmp0/tmp3/t3.txt",
457+
# "tmp1/t2.txt",
458+
# "tmp1/t3.txt"]
459+
#
460+
# If +src+ and +dest+ are both paths to directories,
461+
# creates links <tt>dest/src</tt> and descendents
462+
# pointing to +src+ and its descendents:
463+
#
464+
# FileUtils.rm_r('tmp1')
465+
# Dir.mkdir('tmp1')
466+
# FileUtils.cp_lr('tmp0', 'tmp1')
467+
# # => ["tmp0/tmp2/t0.txt",
468+
# "tmp0/tmp2/t1.txt",
469+
# "tmp0/tmp3/t2.txt",
470+
# "tmp0/tmp3/t3.txt",
471+
# "tmp1/tmp0/tmp2/t0.txt",
472+
# "tmp1/tmp0/tmp2/t1.txt",
473+
# "tmp1/tmp0/tmp3/t2.txt",
474+
# "tmp1/tmp0/tmp3/t3.txt"]
423475
#
424-
# Hard link +src+ to +dest+. If +src+ is a directory, this method links
425-
# all its contents recursively. If +dest+ is a directory, links
426-
# +src+ to +dest/src+.
427-
#
428-
# +src+ can be a list of files.
476+
# Keyword arguments:
429477
#
430-
# If +dereference_root+ is true, this method dereference tree root.
478+
# - <tt>dereference_root: false</tt> - does not follow soft links.
479+
# - <tt>noop: true</tt> - does not create links.
480+
# - <tt>remove_destination: true</tt> - removes +dest+ before creating links.
481+
# - <tt>verbose: true</tt> - prints an equivalent command:
431482
#
432-
# If +remove_destination+ is true, this method removes each destination file before copy.
483+
# FileUtils.cp_lr('tmp0', 'tmp1', verbose: true, noop: true)
484+
# FileUtils.cp_lr(['tmp0/tmp3/t2.txt', 'tmp0/tmp3/t3.txt'], 'tmp1', verbose: true, noop: true)
433485
#
434-
# FileUtils.rm_r site_ruby + '/mylib', force: true
435-
# FileUtils.cp_lr 'lib/', site_ruby + '/mylib'
486+
# Output:
436487
#
437-
# # Examples of linking several files to target directory.
438-
# FileUtils.cp_lr %w(mail.rb field.rb debug/), site_ruby + '/tmail'
439-
# FileUtils.cp_lr Dir.glob('*.rb'), '/home/aamine/lib/ruby', noop: true, verbose: true
488+
# cp -lr tmp0 tmp1
489+
# cp -lr tmp0/tmp3/t2.txt tmp0/tmp3/t3.txt tmp1
440490
#
441-
# # If you want to link all contents of a directory instead of the
442-
# # directory itself, c.f. src/x -> dest/x, src/y -> dest/y,
443-
# # use the following code.
444-
# FileUtils.cp_lr 'src/.', 'dest' # cp_lr('src', 'dest') makes dest/src, but this doesn't.
491+
# Raises an exception if +dest+ is the path to an existing file
492+
# and keyword argument +remove_destination+ is not +true+.
445493
#
446494
def cp_lr(src, dest, noop: nil, verbose: nil,
447495
dereference_root: true, remove_destination: false)

0 commit comments

Comments
 (0)