當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Pathname.descend用法及代碼示例

本文簡要介紹ruby語言中 Pathname.descend 的用法。

用法

descend() { |v| ... }

迭代並為給定路徑中的每個元素按降序生成一個新的 Pathname 對象。

Pathname.new('/path/to/some/file.rb').descend {|v| p v}
   #<Pathname:/>
   #<Pathname:/path>
   #<Pathname:/path/to>
   #<Pathname:/path/to/some>
   #<Pathname:/path/to/some/file.rb>

Pathname.new('path/to/some/file.rb').descend {|v| p v}
   #<Pathname:path>
   #<Pathname:path/to>
   #<Pathname:path/to/some>
   #<Pathname:path/to/some/file.rb>

如果沒有給出塊,則返回 Enumerator

enum = Pathname.new("/usr/bin/ruby").descend
  # ... do stuff ...
enum.each { |e| ... }
  # yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.

它不訪問文件係統。

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Pathname.descend。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。