Skip to content

Commit 72f309f

Browse files
[DOC] Doc for Pathname class (ruby#16352)
1 parent 98e55e7 commit 72f309f

2 files changed

Lines changed: 74 additions & 55 deletions

File tree

lib/pathname.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#
1010
# For documentation, see class Pathname.
1111
#
12-
1312
class Pathname # * Find *
1413
#
1514
# Iterates over the directory tree in a depth first manner, yielding a

pathname_builtin.rb

Lines changed: 74 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,96 @@
11
# frozen_string_literal: true
22
#
3-
# = pathname.rb
3+
# A \Pathname object contains a string directory path or filepath;
4+
# it does not represent a corresponding actual file or directory
5+
# -- which in fact may or may not exist.
46
#
5-
# Object-Oriented Pathname Class
7+
# A \Pathname object is immutable (except for method #freeze).
68
#
7-
# Author:: Tanaka Akira <akr@m17n.org>
8-
# Documentation:: Author and Gavin Sinclair
9+
# A pathname may be relative or absolute:
910
#
10-
# For documentation, see class Pathname.
11+
# Pathname.new('lib') # => #<Pathname:lib>
12+
# Pathname.new('/usr/local/bin') # => #<Pathname:/usr/local/bin>
1113
#
12-
14+
# == Convenience Methods
15+
#
16+
# The class provides *all* functionality from class File and module FileTest,
17+
# along with some functionality from class Dir and module FileUtils.
18+
#
19+
# Here's an example string path and corresponding \Pathname object:
20+
#
21+
# path = 'lib/fileutils.rb'
22+
# pn = Pathname.new(path) # => #<Pathname:lib/fileutils.rb>
23+
#
24+
# Each of these method pairs (\Pathname vs. \File) gives exactly the same result:
25+
#
26+
# pn.size # => 83777
27+
# File.size(path) # => 83777
28+
#
29+
# pn.directory? # => false
30+
# File.directory?(path) # => false
31+
#
32+
# pn.read.size # => 81074
33+
# File.read(path).size# # => 81074
34+
#
35+
# Each of these method pairs gives similar results,
36+
# but each \Pathname method returns a more versatile \Pathname object,
37+
# instead of a string:
38+
#
39+
# pn.dirname # => #<Pathname:lib>
40+
# File.dirname(path) # => "lib"
1341
#
14-
# Pathname represents the name of a file or directory on the filesystem,
15-
# but not the file itself.
42+
# pn.basename # => #<Pathname:fileutils.rb>
43+
# File.basename(path) # => "fileutils.rb"
1644
#
17-
# The pathname depends on the Operating System: Unix, Windows, etc.
18-
# This library works with pathnames of local OS, however non-Unix pathnames
19-
# are supported experimentally.
45+
# pn.split # => [#<Pathname:lib>, #<Pathname:fileutils.rb>]
46+
# File.split(path) # => ["lib", "fileutils.rb"]
2047
#
21-
# A Pathname can be relative or absolute. It's not until you try to
22-
# reference the file that it even matters whether the file exists or not.
48+
# Each of these methods takes a block:
2349
#
24-
# Pathname is immutable. It has no method for destructive update.
50+
# pn.open do |file|
51+
# p file
52+
# end
53+
# File.open(path) do |file|
54+
# p file
55+
# end
2556
#
26-
# The goal of this class is to manipulate file path information in a neater
27-
# way than standard Ruby provides. The examples below demonstrate the
28-
# difference.
57+
# The outputs for each:
2958
#
30-
# *All* functionality from File, FileTest, and some from Dir and FileUtils is
31-
# included, in an unsurprising way. It is essentially a facade for all of
32-
# these, and more.
59+
# #<File:lib/fileutils.rb (closed)>
60+
# #<File:lib/fileutils.rb (closed)>
3361
#
34-
# == Examples
62+
# Each of these methods takes a block:
3563
#
36-
# === Example 1: Using Pathname
64+
# pn.each_line do |line|
65+
# p line
66+
# break
67+
# end
68+
# File.foreach(path) do |line|
69+
# p line
70+
# break
71+
# end
3772
#
38-
# require 'pathname'
39-
# pn = Pathname.new("/usr/bin/ruby")
40-
# size = pn.size # 27662
41-
# isdir = pn.directory? # false
42-
# dir = pn.dirname # Pathname:/usr/bin
43-
# base = pn.basename # Pathname:ruby
44-
# dir, base = pn.split # [Pathname:/usr/bin, Pathname:ruby]
45-
# data = pn.read
46-
# pn.open { |f| _ }
47-
# pn.each_line { |line| _ }
73+
# The outputs for each:
4874
#
49-
# === Example 2: Using standard Ruby
75+
# "# frozen_string_literal: true\n"
76+
# "# frozen_string_literal: true\n"
5077
#
51-
# pn = "/usr/bin/ruby"
52-
# size = File.size(pn) # 27662
53-
# isdir = File.directory?(pn) # false
54-
# dir = File.dirname(pn) # "/usr/bin"
55-
# base = File.basename(pn) # "ruby"
56-
# dir, base = File.split(pn) # ["/usr/bin", "ruby"]
57-
# data = File.read(pn)
58-
# File.open(pn) { |f| _ }
59-
# File.foreach(pn) { |line| _ }
78+
# == More Methods
6079
#
61-
# === Example 3: Special features
80+
# Here is a sampling of other available methods:
6281
#
63-
# p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
64-
# p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
65-
# p3 = p1.parent # Pathname:/usr
66-
# p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
67-
# pwd = Pathname.pwd # Pathname:/home/gavin
68-
# pwd.absolute? # true
69-
# p5 = Pathname.new "." # Pathname:.
70-
# p5 = p5 + "music/../articles" # Pathname:music/../articles
71-
# p5.cleanpath # Pathname:articles
72-
# p5.realpath # Pathname:/home/gavin/articles
73-
# p5.children # [Pathname:/home/gavin/articles/linux, ...]
82+
# p1 = Pathname.new('/usr/lib') # => #<Pathname:/usr/lib>
83+
# p1.absolute? # => true
84+
# p2 = p1 + 'ruby/4.0' # => #<Pathname:/usr/lib/ruby/4.0>
85+
# p3 = p1.parent # => #<Pathname:/usr>
86+
# p4 = p2.relative_path_from(p3) # => #<Pathname:lib/ruby/4.0>
87+
# p4.absolute? # => false
88+
# p5 = Pathname.new('.') # => #<Pathname:.>
89+
# p6 = p5 + 'usr/../var' # => #<Pathname:usr/../var>
90+
# p6.cleanpath # => #<Pathname:var>
91+
# p6.realpath # => #<Pathname:/var>
92+
# p6.children.take(2)
93+
# # => [#<Pathname:usr/../var/local>, #<Pathname:usr/../var/spool>]
7494
#
7595
# == Breakdown of functionality
7696
#

0 commit comments

Comments
 (0)