mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/pathname.rb (Kernel#Pathname): new method.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10303 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
eeda97f8e3
commit
7d274ff6fb
3 changed files with 54 additions and 6 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
Sat Jun 17 14:53:32 2006 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
|
* lib/pathname.rb (Kernel#Pathname): new method.
|
||||||
|
|
||||||
Sat Jun 17 02:01:00 2006 Tanaka Akira <akr@m17n.org>
|
Sat Jun 17 02:01:00 2006 Tanaka Akira <akr@m17n.org>
|
||||||
|
|
||||||
* lib/pp.rb (Kernel#pretty_inspect): defined for pretty printed
|
* lib/pp.rb (Kernel#pretty_inspect): defined for pretty printed
|
||||||
|
|
|
@ -182,12 +182,22 @@
|
||||||
# information. In some cases, a brief description will follow.
|
# information. In some cases, a brief description will follow.
|
||||||
#
|
#
|
||||||
class Pathname
|
class Pathname
|
||||||
|
|
||||||
|
# :stopdoc:
|
||||||
|
if RUBY_VERSION < "1.9"
|
||||||
|
TO_PATH = :to_str
|
||||||
|
else
|
||||||
|
# to_path is implemented so Pathname objects are usable with File.open, etc.
|
||||||
|
TO_PATH = :to_path
|
||||||
|
end
|
||||||
|
# :startdoc:
|
||||||
|
|
||||||
#
|
#
|
||||||
# Create a Pathname object from the given String (or String-like object).
|
# Create a Pathname object from the given String (or String-like object).
|
||||||
# If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
|
# If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
|
||||||
#
|
#
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
path = path.to_path if path.respond_to? :to_path
|
path = path.__send__(TO_PATH) if path.respond_to? TO_PATH
|
||||||
@path = path.dup
|
@path = path.dup
|
||||||
|
|
||||||
if /\0/ =~ @path
|
if /\0/ =~ @path
|
||||||
|
@ -229,7 +239,7 @@ class Pathname
|
||||||
end
|
end
|
||||||
|
|
||||||
# to_path is implemented so Pathname objects are usable with File.open, etc.
|
# to_path is implemented so Pathname objects are usable with File.open, etc.
|
||||||
alias to_path to_s
|
alias_method TO_PATH, :to_s
|
||||||
|
|
||||||
def inspect # :nodoc:
|
def inspect # :nodoc:
|
||||||
"#<#{self.class}:#{@path}>"
|
"#<#{self.class}:#{@path}>"
|
||||||
|
@ -491,9 +501,10 @@ class Pathname
|
||||||
# Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
|
# Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
|
||||||
# # yields "usr", "bin", and "ruby".
|
# # yields "usr", "bin", and "ruby".
|
||||||
#
|
#
|
||||||
def each_filename # :yield: s
|
def each_filename # :yield: filename
|
||||||
prefix, names = split_names(@path)
|
prefix, names = split_names(@path)
|
||||||
names.each {|filename| yield filename }
|
names.each {|filename| yield filename }
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Iterates over and yields a new Pathname object
|
# Iterates over and yields a new Pathname object
|
||||||
|
@ -514,6 +525,8 @@ class Pathname
|
||||||
#
|
#
|
||||||
# It doesn't access actual filesystem.
|
# It doesn't access actual filesystem.
|
||||||
#
|
#
|
||||||
|
# This method is available since 1.8.5.
|
||||||
|
#
|
||||||
def descend
|
def descend
|
||||||
vs = []
|
vs = []
|
||||||
ascend {|v| vs << v }
|
ascend {|v| vs << v }
|
||||||
|
@ -539,6 +552,8 @@ class Pathname
|
||||||
#
|
#
|
||||||
# It doesn't access actual filesystem.
|
# It doesn't access actual filesystem.
|
||||||
#
|
#
|
||||||
|
# This method is available since 1.8.5.
|
||||||
|
#
|
||||||
def ascend
|
def ascend
|
||||||
path = @path
|
path = @path
|
||||||
yield self
|
yield self
|
||||||
|
@ -1031,3 +1046,13 @@ class Pathname # * mixed *
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Kernel
|
||||||
|
# create a pathname object.
|
||||||
|
#
|
||||||
|
# This method is available since 1.8.5.
|
||||||
|
def Pathname(path) # :doc:
|
||||||
|
Pathname.new(path)
|
||||||
|
end
|
||||||
|
private :Pathname
|
||||||
|
end
|
||||||
|
|
|
@ -5,8 +5,16 @@ require 'pathname'
|
||||||
|
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
|
require 'enumerator'
|
||||||
|
|
||||||
class TestPathname < Test::Unit::TestCase
|
class TestPathname < Test::Unit::TestCase
|
||||||
|
|
||||||
|
if RUBY_VERSION < "1.9"
|
||||||
|
FUNCALL = :__send__
|
||||||
|
else
|
||||||
|
FUNCALL = :funcall
|
||||||
|
end
|
||||||
|
|
||||||
def self.define_assertion(name, &block)
|
def self.define_assertion(name, &block)
|
||||||
@defassert_num ||= {}
|
@defassert_num ||= {}
|
||||||
@defassert_num[name] ||= 0
|
@defassert_num[name] ||= 0
|
||||||
|
@ -115,7 +123,7 @@ class TestPathname < Test::Unit::TestCase
|
||||||
|
|
||||||
# has_trailing_separator?(path) -> bool
|
# has_trailing_separator?(path) -> bool
|
||||||
def has_trailing_separator?(path)
|
def has_trailing_separator?(path)
|
||||||
Pathname.allocate.funcall(:has_trailing_separator?, path)
|
Pathname.allocate.send(FUNCALL, :has_trailing_separator?, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
defassert(:has_trailing_separator?, false, "/")
|
defassert(:has_trailing_separator?, false, "/")
|
||||||
|
@ -124,11 +132,11 @@ class TestPathname < Test::Unit::TestCase
|
||||||
defassert(:has_trailing_separator?, true, "a/")
|
defassert(:has_trailing_separator?, true, "a/")
|
||||||
|
|
||||||
def add_trailing_separator(path)
|
def add_trailing_separator(path)
|
||||||
Pathname.allocate.funcall(:add_trailing_separator, path)
|
Pathname.allocate.send(FUNCALL, :add_trailing_separator, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def del_trailing_separator(path)
|
def del_trailing_separator(path)
|
||||||
Pathname.allocate.funcall(:del_trailing_separator, path)
|
Pathname.allocate.send(FUNCALL, :del_trailing_separator, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
defassert(:del_trailing_separator, "/", "/")
|
defassert(:del_trailing_separator, "/", "/")
|
||||||
|
@ -313,6 +321,10 @@ class TestPathname < Test::Unit::TestCase
|
||||||
assert_equal(p1, p2)
|
assert_equal(p1, p2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_initialize_nul
|
||||||
|
assert_raise(ArgumentError) { Pathname.new("a\0") }
|
||||||
|
end
|
||||||
|
|
||||||
class AnotherStringLike # :nodoc:
|
class AnotherStringLike # :nodoc:
|
||||||
def initialize(s) @s = s end
|
def initialize(s) @s = s end
|
||||||
def to_str() @s end
|
def to_str() @s end
|
||||||
|
@ -374,6 +386,9 @@ class TestPathname < Test::Unit::TestCase
|
||||||
assert_equal(nil, "a" <=> Pathname.new("a"))
|
assert_equal(nil, "a" <=> Pathname.new("a"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def pathsub(path, pat, repl) Pathname.new(path).sub(pat, repl).to_s end
|
||||||
|
defassert(:pathsub, "a.o", "a.c", /\.c\z/, ".o")
|
||||||
|
|
||||||
def root?(path)
|
def root?(path)
|
||||||
Pathname.new(path).root?
|
Pathname.new(path).root?
|
||||||
end
|
end
|
||||||
|
@ -464,4 +479,8 @@ class TestPathname < Test::Unit::TestCase
|
||||||
Pathname.new("/usr/bin/ruby").each_filename {|f| result << f }
|
Pathname.new("/usr/bin/ruby").each_filename {|f| result << f }
|
||||||
assert_equal(%w[usr bin ruby], result)
|
assert_equal(%w[usr bin ruby], result)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_kernel_pathname
|
||||||
|
assert_equal(Pathname.new("a"), Pathname("a"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue