Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: adbdeab9d6f1
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 739df8915187
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Dec 2, 2015

  1. Copy the full SHA
    1702f89 View commit details
  2. Merge pull request #3510 from lucasallan/pathname-enumerator

    [Ruby-2.3] - Pathname#descend and Pathname#ascend supported blockless…
    kares committed Dec 2, 2015
    Copy the full SHA
    739df89 View commit details
Showing with 26 additions and 2 deletions.
  1. +16 −0 lib/ruby/stdlib/pathname.rb
  2. +10 −2 test/mri/pathname/test_pathname.rb
16 changes: 16 additions & 0 deletions lib/ruby/stdlib/pathname.rb
Original file line number Diff line number Diff line change
@@ -283,9 +283,24 @@ def each_filename # :yield: filename
# #<Pathname:path/to/some>
# #<Pathname:path/to/some/file.rb>
#
# Returns an Enumerator if no block was given.
#
# enum = Pathname.new("/usr/bin/ruby").descend
# # ... do stuff ...
# enum.each { |e| ... }
# # yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
#
# Returns an Enumerator if no block was given.
#
# enum = Pathname.new("/usr/bin/ruby").ascend
# # ... do stuff ...
# enum.each { |e| ... }
# # yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
#
# It doesn't access the filesystem.
#
def descend
return to_enum(__method__) unless block_given?
vs = []
ascend {|v| vs << v }
vs.reverse_each {|v| yield v }
@@ -311,6 +326,7 @@ def descend
# It doesn't access the filesystem.
#
def ascend
return to_enum(__method__) unless block_given?
path = @path
yield self
while r = chop_basename(path)
12 changes: 10 additions & 2 deletions test/mri/pathname/test_pathname.rb
Original file line number Diff line number Diff line change
@@ -431,7 +431,7 @@ def test_realdirpath
end

def descend(path)
Pathname.new(path).enum_for(:descend).map {|v| v.to_s }
Pathname.new(path).descend.map(&:to_s)
end

defassert(:descend, %w[/ /a /a/b /a/b/c], "/a/b/c")
@@ -440,14 +440,22 @@ def descend(path)
defassert(:descend, %w[a/], "a/")

def ascend(path)
Pathname.new(path).enum_for(:ascend).map {|v| v.to_s }
Pathname.new(path).ascend.map(&:to_s)
end

defassert(:ascend, %w[/a/b/c /a/b /a /], "/a/b/c")
defassert(:ascend, %w[a/b/c a/b a], "a/b/c")
defassert(:ascend, %w[./a/b/c ./a/b ./a .], "./a/b/c")
defassert(:ascend, %w[a/], "a/")

def test_blockless_ascend_is_enumerator
assert_kind_of(Enumerator, Pathname.new('a').ascend)
end

def test_blockless_descend_is_enumerator
assert_kind_of(Enumerator, Pathname.new('a').descend)
end

def test_initialize
p1 = Pathname.new('a')
assert_equal('a', p1.to_s)