Feature #7360 ยป 19c4ae36aaea65b30e8b73c89f7ac42bd6905ed7.patch
ext/pathname/lib/pathname.rb | ||
---|---|---|
535 | 535 |
Pathname.new(File.join(*relpath_names)) |
536 | 536 |
end |
537 | 537 |
end |
538 | ||
539 |
# |
|
540 |
# Returns or yields Pathname objects that match the given glob |
|
541 |
# pattern or glob patterns, relative to +self+. |
|
542 |
# |
|
543 |
# It joins +self+ with the given glob pattern. +self+ should be a |
|
544 |
# directory. When +pattern+ is an absolute path, +self+ does not |
|
545 |
# matter. |
|
546 |
# |
|
547 |
# Pathname('config').glob("*.rb") |
|
548 |
# #=> [#<Pathname:config/environment.rb>, #<Pathname:config/routes.rb>, ..] |
|
549 |
# |
|
550 |
# Pathname('config').glob(["*.rb", "*.txt"]) |
|
551 |
# #=> [#<Pathname:config/environment.rb>, #<Pathname:config/routes.rb>, ..] |
|
552 |
# |
|
553 |
# See Dir.glob. |
|
554 |
# |
|
555 |
def glob(pattern, flags = 0, &b) |
|
556 |
Array(pattern).flat_map do |pat| |
|
557 |
self.class.glob(join(pat), flags, &b) |
|
558 |
end |
|
559 |
end |
|
538 | 560 |
end |
539 | 561 | |
540 | 562 |
test/pathname/test_pathname.rb | ||
---|---|---|
1222 | 1222 |
} |
1223 | 1223 |
end |
1224 | 1224 | |
1225 |
def test_glob |
|
1226 |
with_tmpchdir('rubytest-pathname') {|dir| |
|
1227 |
open("f", "w") {|f| f.write "abc" } |
|
1228 |
Dir.mkdir("d") |
|
1229 |
assert_equal([Pathname("d"), Pathname("f")], Pathname('').glob("*").sort) |
|
1230 |
a = [] |
|
1231 |
Pathname('').glob("*") {|path| a << path } |
|
1232 |
a.sort! |
|
1233 |
assert_equal([Pathname("d"), Pathname("f")], a) |
|
1234 |
open("FOO.txt", "w") {|f| f.write "abc" } |
|
1235 |
assert_equal([Pathname("FOO.txt")], Pathname('').glob("foo.*", File::FNM_CASEFOLD)) |
|
1236 |
} |
|
1237 |
with_tmpchdir('rubytest-pathname') {|dir| |
|
1238 |
open("a.rb", "w") {|f| f.write "abc" } |
|
1239 |
open("a.txt", "w") {|f| f.write "abc" } |
|
1240 |
assert_equal([Pathname("a.rb"), Pathname("a.txt")], Pathname('').glob(["*.rb", "*.txt"])) |
|
1241 |
} |
|
1242 |
end |
|
1243 | ||
1225 | 1244 |
def test_s_getwd |
1226 | 1245 |
wd = Pathname.getwd |
1227 | 1246 |
assert_kind_of(Pathname, wd) |