Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Pathname#each_filename

each_filename() { |filename| ... }

Iterates over each component of the path.

Pathname.new("/usr/bin/ruby").each_filename {|filename| ... }
  # yields "usr", "bin", and "ruby".

Returns an Enumerator if no block was given.

enum = Pathname.new("/usr/bin/ruby").each_filename
  # ... do stuff ...
enum.each { |e| ... }
  # yields "usr", "bin", and "ruby".
See also #each_entry #each_line #each_child #empty? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0