summaryrefslogtreecommitdiff
path: root/lib/pathname.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-10-03 16:36:24 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-10-03 16:36:24 +0000
commitf047918d094bba0379149bb839d274ec243c951c (patch)
tree04131fb8a06cf3f743f02e87e0292a43557ff2ae /lib/pathname.rb
parent29e64f7d3613f425f36cf7a617f319f9a3aabc6c (diff)
* lib/pathname.rb (initialize): duplicate and freeze argument.
(to_s): return duplicated string. (children): new method. (each_line): new alias to foreachline. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4662 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/pathname.rb')
-rw-r--r--lib/pathname.rb27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/pathname.rb b/lib/pathname.rb
index d48cef6857..1182a975ed 100644
--- a/lib/pathname.rb
+++ b/lib/pathname.rb
@@ -2,9 +2,14 @@
#
# Author:: Tanaka Akira <akr@m17n.org>
+# Pathname represents a pathname which locates a file in a filesystem.
+#
+# Pathname is immutable. It has no method for destructive update.
+#
class Pathname
def initialize(path)
- @path = path.to_str
+ @path = path.to_str.dup
+ @path.freeze
end
def ==(other)
@@ -24,7 +29,7 @@ class Pathname
end
def to_s
- @path
+ @path.dup
end
alias to_str to_s
@@ -230,11 +235,22 @@ class Pathname
end
end
+ # Pathname#children returns the children of the directory as an array of
+ # pathnames. I.e. it is similar to Pathname#entries except '.' and '..'
+ # is not returned.
+ def children
+ Dir.entries(@path).map {|f|
+ f == '.' || f == '..' ? nil : Pathname.new(f)
+ }.compact
+ end
+
end
# IO
class Pathname
def foreachline(*args, &block) IO.foreach(@path, *args, &block) end
+ alias each_line foreachline
+
def read(*args) IO.read(@path, *args) end
def readlines(*args) IO.readlines(@path, *args) end
def sysopen(*args) IO.sysopen(@path, *args) end
@@ -481,7 +497,6 @@ if $0 == __FILE__
assert_equal('a/b/../../../../c/../d', Pathname.new('a/b/../../../../c/../d').cleanpath(true).to_s)
end
-
def test_cleanpath_no_symlink
assert_equal('/', Pathname.new('/').cleanpath.to_s)
assert_equal('/', Pathname.new('//').cleanpath.to_s)
@@ -521,5 +536,11 @@ if $0 == __FILE__
assert_equal('../../d', Pathname.new('a/b/../../../../c/../d').cleanpath.to_s)
end
+ def test_destructive_update
+ path = Pathname.new("a")
+ path.to_s.replace "b"
+ assert_equal(Pathname.new("a"), path)
+ end
+
end
end