walk_a_dir_non_recursively.sf 430 B

1234567891011121314151617181920212223
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Walk_a_directory/Non-recursively#Sidef
  4. #
  5. func file_match(Block callback, pattern=/\.txt\z/, path=Dir.cwd) {
  6. path.open(\var dir_h) || return();
  7. dir_h.entries.each { |entry|
  8. if (entry.basename ~~ pattern) {
  9. callback(entry);
  10. }
  11. }
  12. }
  13. file_match(
  14. path: %d'/tmp',
  15. pattern: /\.p[lm]\z/i,
  16. callback: { |file|
  17. say file;
  18. }
  19. );